Skip to content

BitonicSort: Dispatch a complete sort in one compute pass. - #35275

Merged
mrdoob merged 1 commit into
mrdoob:devfrom
subashc2023:bitonic-one-pass
Oct 30, 2026
Merged

mrdoob merged 1 commit into
mrdoob:devfrom
subashc2023:bitonic-one-pass

Conversation

@subashc2023

@subashc2023 subashc2023 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Related issue: -

Description

BitonicSort.compute() calls computeStep() once per step, and each step calls renderer.compute() twice: once for its swap, and once for setAlgoFn, which moves the sort's state in infoStorage on to the next step (BitonicSort.js). On WebGPU every renderer.compute() call begins its own compute pass in a new command encoder and submits it (WebGPUBackend.js). Sorting 4M keys with a workgroup size of 256 takes 105 steps, so one sort makes 211 submits, and the time between them adds up to far more than the sort itself.

The sort's state lives on the GPU, so on the CPU a step only decides which shaders run next. This PR moves that into a private _getStepNodes() that returns the step's shaders instead of dispatching them. computeStep() passes them to a single renderer.compute() call, and compute() collects the shaders of every step and passes them all to one call, so a whole sort runs in one compute pass. Dispatches in a pass run in order and see the storage writes of the ones before them, as separate submits did. The shaders, their order and the state computeStep() leaves behind are unchanged.

Tested on dev in Chrome on WebGPU, with a page that runs this version and the current one on the same random keys, from 1K to 4M keys with workgroup sizes of 64 and 256. Both dispatch the same shaders in the same order, from compute(), from computeStep() and from compute() after a few computeStep() calls, and every complete sort matches a sort on the CPU. webgpu_compute_sort_bitonic sorts as before, with one renderer.compute() call per step instead of two.

In a benchmark that sorts 4M keys every frame, compute() now makes one renderer.compute() call instead of 211, and the frame time goes from about 14 ms to about 2 ms. The GPU time stays within a few percent. This was timed on r186 with the change patched in, as r186's BitonicSort steps are the same as dev's.

Screenshots

webgpu_compute_sort_bitonic is on the E2E exception list, so here is its left canvas, the one that uses BitonicSort, on dev and with this PR. They were taken with a copy of the example that seeds Math.random() and runs computeStep() when asked instead of on a timer, so both sides sort the same keys. All 37 frames, the input and one after each of the 36 steps, are identical pixel for pixel.

Step dev This PR
0 (input) dev, step 0 This PR, step 0
2 dev, step 2 This PR, step 2
18 dev, step 18 This PR, step 18
36 (sorted) dev, step 36 This PR, step 36

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🖼️ E2E screenshot tests

✅ All examples render correctly again (run).

@subashc2023

Copy link
Copy Markdown
Contributor Author

E2E

webgpu_camera_logarithmicdepthbuffer failed E2E here and on other PRs, captured black or one frame early. It initializes two WebGPURenderers one after the other once its font has loaded, and on the software rasterizer CI uses, the second one's requestDevice() can still be pending when the harness opens its render gate, 2 seconds after the network went idle. The first renderer's animation loop has requested a frame by then, and that frame ends the render before the example draws. The last commit makes the harness also wait for pending adapter and device requests, as it waits for videos. Locally, with the second device delayed, the example failed in both of those ways without the change and matched its screenshot with it, and 38 of 40 WebGPU examples run through both versions gave the same results (the other two are on the exception list). It can go in its own PR if you prefer.

@mrdoob

mrdoob commented Oct 30, 2026

Copy link
Copy Markdown
Owner

Can you rebase this on top of dev? I wonder if #35278 solves the E2E issue here.

@mrdoob mrdoob added this to the r187 milestone Oct 30, 2026
@mrdoob
mrdoob merged commit d678ae0 into mrdoob:dev Oct 30, 2026
9 checks passed
@subashc2023
subashc2023 deleted the bitonic-one-pass branch October 30, 2026 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants