WebGPU: creating render pipelines asynchronously, so a new material does not stall the frame that first draws it

This follows 64298 #11, where this was suggested as a separate discussion.

On WebGPU, the first draw that needs a new render pipeline creates it synchronously, and that frame waits for it. In our test scene, a wave of meshes with a PBR variant no draw had used yet costs 108 to 129 ms in the frame that first draws it. #19090 adds an opt-in, engine.asynchronousPipelineCreation (off by default): a mesh draw whose pipeline is not created yet is omitted while the pipeline is created asynchronously, and the mesh shows a few frames later. With the option off, nothing changes.

Following #11:

  • On the existing path. No second cache: on a miss, tryGetRenderPipeline() calls the existing preWarmPipeline(), which now creates a configuration once (a second request for the same cache token, from a draw or from createRenderPipelineAsync(), gets the pending promise), reports a failed creation, after which the next miss creates the pipeline synchronously so the WebGPU error shows as today, and ignores completions once the device is replaced or the engine disposed.
  • Incomplete frames. Only mesh draws can wait, and only in the passes the scene renders again (its cameras, the render targets of its frame, the frame graph). Post-processes, a render target rendered once, a screenshot and any render outside the scene’s frame create their pipelines synchronously, so a pass that runs once never misses a mesh. A mesh shows in all its passes together (shadow maps, depth, geometry buffer, effect layers included). A snapshot never replays a frame that misses a draw: a recording that omitted draws is replayed while their pipelines are created and recorded again at the first frame after, and a recording that would show a mesh in some passes only is recorded again at the next frame. scene.isReady() is false while pipelines are being created.
  • Compared with prewarming known content. Prewarming each wave as an app would (createRenderPipelineAsync({ effect, mesh }) once its effect is ready) also removes the long frames, but here it does not create the pipeline the wave draws with: the new meshes receive a shadow map that is a depth texture, so their draws use texture state 1, and createRenderPipelineAsync() always prewarms texture state 0. The automatic path creates exactly the pipelines the draws ask for, in every pass, without knowing the content in advance; the price is the omitted draws.
  • The name. It is asynchronous pipeline creation: the browser may still compile part of it elsewhere or at first use, and nothing here claims the whole cost left the main thread.

Measured on 9.33.0 and this PR, the machine checked quiet before and during each measure (Chromium 147, RTX 3080 Laptop GPU, 800×450): 600 meshes with a 4-cascade CSM, and every 40 frames a wave of 20 meshes with a new PBR variant, 8 waves, a new browser for every run, 2 rounds in alternating order. A frame’s time runs from its start until the GPU finished it (onSubmittedWorkDone).

Slowest frame (no snapshot / compatibilityMode = false / FAST snapshot) Frames over 100 ms, of 8 waves Pipelines created inside a frame A wave shows
9.33.0 118-121 / 108-116 / 122-129 ms 5-8 8 at once
this PR, the option on 33-34 / 31-35 / 35-45 ms 0 0 5-6 / 6-7 / 8-11 frames later
this PR, the app prewarms each wave 34 / 27-32 / 38-42 ms 0 8 4-5 / 5-6 / 11-14 frames later

Once a wave shows, every arm draws the same image as 9.33.0, in the three modes; with the option off the PR draws as 9.33.0 in every frame. With nothing pending the option costs nothing measurable (CPU per frame 7.74 ms on and 7.76 off with compatibilityMode = false, 10.24 and 9.90 without a snapshot, within the spread between rounds; with it off, the PR matches 9.33.0).

A Playground of the same waves, which counts the pipelines the device creates: https://playground.babylonjs.com/#INWH19#0. On 9.33.0 with WebGPU the slowest frame of a wave is 86 to 103 ms, and each wave creates its pipeline synchronously (9 for 8 waves); with WebGL2, which links programs in parallel before a mesh draws, it is 26 to 43 ms. On a build with the option, it runs the waves again with it on: on this PR, 21 ms, with the 8 pipelines created asynchronously and none synchronously.

One question: would a separate change letting createRenderPipelineAsync() take the texture state (or derive it from a sub mesh) be welcome? It would make prewarming match the draws that receive depth-texture shadows.

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

cc @Evgeni_Popov

Thanks for separating this out and building on the existing prewarming/cache path. The opt-in approach makes sense for unexpected pipeline misses, provided the application accepts temporarily missing geometry. The deduplication, error reporting, and snapshot invalidation address important parts of the earlier feedback.

On your question: yes, a separate change allowing createRenderPipelineAsync() to prewarm the appropriate texture state would be welcome. The current implementation passes 0, whereas the draw path derives the state from the bound textures, so the mismatch you describe is supported by the code. This also applies to some unfilterable float textures, not just depth-texture shadows. Ideally, any automatic derivation would share the draw path’s logic rather than introduce another version of it. That change would be useful independently of automatic asynchronous creation.

For the automatic path, I think a couple of correctness points still need attention:

  • “A mesh shows in all its passes together” needs qualification. As your PR description acknowledges, an earlier pass can draw the mesh before a later pass discovers a missing pipeline. Invalidating the snapshot prevents that inconsistent frame from being replayed indefinitely, but does not prevent the initial frame from containing, for example, a shadow without its visible caster. Please make that limitation explicit in the feature’s documented behavior.

  • Please cover render targets with refreshRate > 1, particularly shadow maps. Protecting render-once targets is necessary, but an intermittently refreshed target can also retain an incomplete result: its draw is omitted while a pipeline is pending, then the main pass resumes drawing the mesh before that target refreshes again. The current/previous-frame mesh markers do not establish that the target has caught up. This needs an explicit strategy—such as forcing an appropriate refresh or conservatively keeping those passes synchronous—and a regression case.

The performance results are encouraging, although I have not independently reproduced them. Please keep queue-completion timing distinct from displayed frame pacing, and report the time until the complete content becomes visible alongside the reduction in long frames.

One detail in the public Playground comparison: the disabled run uses variants 0–7, while the enabled run uses 8–15, which enables anisotropy throughout the second run. It also always runs the disabled case first. Using identical variants in separate fresh runs, with alternating order, would make that comparison stronger.

Overall, I’m supportive of the direction. The prewarming improvement can stand on its own; the automatic path needs the retained-target behavior and transient cross-pass limitations nailed down.

Thanks for going through it this closely.

Prewarming the texture state. Good, we’ll send it as its own PR, apart from #19090. The draw path’s derivation (in _draw, a bit for each texture of the shader that is a depth texture, or a float texture the device can’t filter linearly) would move into one helper that createRenderPipelineAsync() also calls, so there’s only one version of it.

“A mesh shows in all its passes together.” You’re right, my post said it without the exception. The option’s doc only had it in a parenthesis (“in that first frame, the passes drawn before the wait show it”), and deca66d makes it a limitation of its own: in the frame where a draw of a mesh first waits, the passes rendered before that draw have already drawn the mesh, so that one frame can show, for instance, a shadow without its caster. From the next frame on, the mesh is held back in all its passes until none of its draws waits.

Render targets with refreshRate > 1. Right, #19090 only covered the targets rendered once. With a2780c0, while mesh draws wait and until those meshes are drawn again, a target refreshed every N frames renders in every frame, like the ones refreshed every frame. So it holds a mesh back in the same frames as the other passes, draws it in the same frame as the main pass, and then goes back to its rate. The price is extra renders of those targets, only while pipelines are being created. With a snapshot on, every target already renders in every frame, so this only changes rendering without one. Two tests go through the real draw path with a shadow map refreshed every 4 frames: in one the mesh waits in the shadow map, in the other in the main pass. Before the change both fail in the frame after the first wait, where the shadow map wasn’t rendered: it kept the render that omitted the mesh, or the shadow of a mesh the main pass was holding back.

Timing. Agreed. The “slowest frame” column was queue completion, from a frame’s start until onSubmittedWorkDone(), not what reached the screen. Here are the waves again, with the time until a wave is complete next to the long frames. They come from the Playground below: 300 boxes, and every 40 frames a wave of 20 spheres with a PBR variant no draw has used yet, 8 waves. Chrome 156, RTX 3080 Laptop GPU, WebGPU, 800×450, a new browser for every run, 4 rounds in alternating order, after one run that isn’t kept (the GPU driver keeps compiled shaders across browsers). A wave is complete when the GPU has finished the first frame that draws all of it, counted from the wave’s creation, so that’s queue completion too; the screen shows it after that. For the 7 waves after the first:

Slowest frame: start to start / start to GPU done Frames over 50 ms (start to start), of 7 waves Wave complete, from its creation to GPU done: median (range)
9.33.0 83-88 / 107-114 ms 7 108 ms (90-121)
#19090, the option off 80-117 / 103-138 ms 7 107 ms (87-147)
#19090, the option on 25-33 / 38-50 ms 0 140 ms (112-179)

So the option takes the stall out of the frames, and a wave is complete about 32 ms later at the median, with a wider spread. The first wave is apart because it also creates, in the frame, the pipeline of the post-process that expands the BRDF table, which stays synchronous since it’s rendered once. It’s complete after 150-170 ms on 9.33.0 and 171-279 ms with the option. On 9.33.0, 9 pipelines are created inside a frame (the 8 waves and that post-process); with the option, 1 is, and the 8 others are created asynchronously.

The Playground. Good catch, you’re right: the run with the option on used variants 8 to 15, so anisotropy was on in all of it, and the run with it off always went first. https://playground.babylonjs.com/#INWH19#1 runs one mode per page, set at its top, with variants 0 to 7 in both modes. It also reports when each wave is first drawn whole: how many frames after it appeared, and the time from its creation until the GPU finished that frame. The table above comes from it.

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

Thanks for the updates. The explicit first-frame limitation is clearer, and the refresh-rate fix addresses the retained-target issue I raised. Covering both a wait in the shadow pass and a wait in the main pass, then checking that normal refresh scheduling resumes, is useful.

One qualification about the implementation: the refresh override is engine-wide, because ObjectRenderer.shouldRender() checks the engine’s last pipeline-wait frame. Unrelated targets with refreshRate > 1 can therefore render every frame too. The override also lasts through the recovery frame, not only while compilation is pending. That seems like a reasonable conservative approach for correctness, but please document that cost explicitly.

It would be useful to include a focused measurement with an intermittently refreshed shadow map. The revised Playground isolates the pipeline-creation benefit, but does not measure the extra render-target work introduced by this fix.

The updated results make the tradeoff much clearer: in this workload, the option reduces long frame intervals, while the complete wave finishes on the GPU about 32 ms later at the median. That is useful information for applications deciding whether smoother ongoing rendering is worth delayed appearance. Please keep the queue-completion and warmed-driver qualifications alongside those figures.

The revised Playground addresses the variant mismatch and ordering issue from the previous version, and extracting the texture-state derivation into one helper for the separate prewarming PR sounds good.

Overall, these changes address the specific concerns from my previous reply. The remaining point here is to make the broader refresh cost explicit and quantify it in a representative shadow-map case.

Thanks. Both points are in #19090 now.

The cost, documented. d34a2786ec (comments only) says it on asynchronousPipelineCreation and on ObjectRenderer.shouldRender(). While mesh draws wait, every render target refreshed every N frames renders in every frame. That includes the targets that don’t draw the meshes that wait, since the engine only knows that a draw waited. It lasts up to the frame in which those meshes are drawn again, two frames after the last wait, so the recovery frame is included. While pipelines are being created, those extra renders are the cost of the option. The same commit brings the list of passes rendered once up to date with the review: a GPU picking, and an object renderer rendered by a direct call to render() that its shouldRender() didn’t ask for.

The measurement. It’s the same waves as before, with one change: the boxes and the spheres cast into a ShadowGenerator (1024) whose shadow map refreshes every 4 frames, and the boxes receive the shadows. Per wave, it counts the shadow map’s renders and the CPU time they take (from its onBeforeRenderObservable to its onAfterRenderObservable). It also measures the CPU time of each frame, from its start to the end of scene.render(). The setup is the same as before: #19090 at 6b5be442c3 (d34a2786ec only adds the comments), Chrome 156, RTX 3080 Laptop GPU, WebGPU, 800×450. Each run gets a new browser, there are 4 rounds in alternating order, and one run before them isn’t kept. Over the 8 waves, 320 frames:

Shadow map renders: all / per wave Shadow map CPU, all the waves Frame CPU: median / mean
the option off 80 / 10 (its rate) 135-153 ms 3.0-3.4 / 3.8-4.6 ms
the option on 202 / 18-31 280-310 ms 3.7-4.0 / 4.2-4.7 ms

With the option, the shadow map renders 2.5 times as often over the waves: 122 extra renders in every run. Paired by round, they take 145-157 ms more CPU over the 320 frames, which is 0.45-0.49 ms per frame on average, or 1.2-1.3 ms per extra render here. The later waves force more of them (18-20 in the first wave, 30-31 in the last), since their variants take longer to create.

Two qualifications:

  • These are counts of frames, and the display ran at 240 Hz. A creation of the same length spans more frames at a higher frame rate, so at 60 Hz the same waves would force fewer extra renders.
  • The CPU times are measured in the page with performance.now(). They aren’t GPU times.

The Playground: https://playground.babylonjs.com/#ZA88KK#0

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

Thanks for documenting the behavior and measuring the additional shadow-map rendering cost. That addresses the points I raised, and the qualifications make the tradeoff clear. Nothing further from me on these points.