Hi wonderful community!
We’re using @babylonjs/core 9.37.0 with ES-module imports and investigating a possible WebGPU initialization race.
Sometimes, when starting the viewer, we came across the following error:
chunk-CrSiKdGA.js:448 Uncaught TypeError: Cannot read properties of null (reading 'shaderProcessingContext')
at h._setVertexState (chunk-CrSiKdGA.js:448:35686)
at h._lookupRenderPipeline (chunk-CrSiKdGA.js:448:25101)
at h.getRenderPipeline (chunk-CrSiKdGA.js:448:25827)
at Nt.clear (chunk-CrSiKdGA.js:448:60556)
at h._clearFullQuad (chunk-CrSiKdGA.js:460:37927)
at h.clear (chunk-CrSiKdGA.js:460:37347)
at l$2._clear (chunk-CEzpGvQq2.js:1:129026)
at l$2._clearFrameBuffer (chunk-CEzpGvQq2.js:1:123130)
at l$2._renderFrame (chunk-CEzpGvQq2.js:1:134775)
at l$2.render (chunk-CEzpGvQq2.js:1:132421)
at _renderLoop (chunk-S8T3cpe4.js:1132:68410)
at h._renderFrame (chunk-bAd6VXV2.js:14:4310)
at h._processFrame (chunk-bAd6VXV2.js:14:4049)
at h._processFrame (chunk-CrSiKdGA.js:466:11741)
at h._renderLoop (chunk-bAd6VXV2.js:14:4102)
at _boundRenderFunction (chunk-bAd6VXV2.js:14:11901)
The WebGPU setup documentation shows that awaiting initialization is sufficient before using the engine:
const engine = new WebGPUEngine(canvas);
await engine.initAsync();
However, looking at the implementation:
initAsync()constructsWebGPUClearQuad.- Its constructor creates an effect with an asynchronous callback that imports the WGSL clear shaders.
initAsync()does not appear to await that effect’s readiness.WebGPUClearQuad.clear()uses the effect to obtain a render pipeline without checking whether it is ready.
This seems to leave a window where initialization has resolved but the internal clear effect is still being prepared. Could starting rendering immediately encounter an unprepared pipeline?
Our current workaround is a patch in initAsync to await for the clearQuad effect compilation:
function waitForWebGPUClearEffect(engine: WebGPUEngine): Promise<void> {
const clearEffect: Effect = engine['_clearQuad']['_effect'];
return new Promise<void>((resolve, reject) => {
if (clearEffect.isReady()) {
resolve();
return;
}
const error = clearEffect.getCompilationError();
if (error) {
reject(new Error(`WebGPU clear shader compilation failed: ${error}`));
return;
}
let compiledObserver: Observer<Effect> | null = null;
let errorObserver: Observer<Effect> | null = null;
compiledObserver = clearEffect.onCompileObservable.addOnce(() => {
clearEffect.onErrorObservable.remove(errorObserver);
resolve();
});
errorObserver = clearEffect.onErrorObservable.addOnce(effect => {
clearEffect.onCompileObservable.remove(compiledObserver);
reject(new Error(`WebGPU clear shader compilation failed: ${effect.getCompilationError()}`));
});
});
}
const originalWebGPUInitAsync = WebGPUEngine.prototype.initAsync;
WebGPUEngine.prototype.initAsync = async function (this: WebGPUEngine, ...args): Promise<void> {
await originalWebGPUInitAsync.call(this, ...args);
await waitForWebGPUClearEffect(this);
};
As I said, it is a sometimes error, due to a possible initialization race, so, in theory, with enough bad luck, you can reproduce it on any WebGPU playground… (I sometimes get it 4 times in a row, then not for several days…).
The same asynchronous clear-shader loading and non-awaiting initialization structure is present in 9.32.0: Babylon.js/packages/dev/core/src/Engines/WebGPU/webgpuClearQuad.pure.ts at 9.32.0 · BabylonJS/Babylon.js · GitHub.
It differs from the 9.0.0 version: Babylon.js/packages/dev/core/src/Engines/WebGPU/webgpuClearQuad.ts at 9.0.0 · BabylonJS/Babylon.js · GitHub.
Hope this helps, the fix seems to work (hard to test because quite complicated to reproduce), I’d gladly take your opinion / thoughts / criticisms on this approach ![]()
Have a good one!!