Repository navigation
BitonicSort: Dispatch a complete sort in one compute pass. - #35275
Conversation
🖼️ E2E screenshot tests✅ All examples render correctly again (run). |
|
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. |
|
Can you rebase this on top of |
0ad66af to
1dd2e13
Compare
Related issue: -
Description
BitonicSort.compute()callscomputeStep()once per step, and each step callsrenderer.compute()twice: once for its swap, and once forsetAlgoFn, which moves the sort's state ininfoStorageon to the next step (BitonicSort.js). On WebGPU everyrenderer.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 singlerenderer.compute()call, andcompute()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 statecomputeStep()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(), fromcomputeStep()and fromcompute()after a fewcomputeStep()calls, and every complete sort matches a sort on the CPU.webgpu_compute_sort_bitonicsorts as before, with onerenderer.compute()call per step instead of two.In a benchmark that sorts 4M keys every frame,
compute()now makes onerenderer.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'sBitonicSortsteps are the same as dev's.Screenshots
webgpu_compute_sort_bitonicis on the E2E exception list, so here is its left canvas, the one that usesBitonicSort, on dev and with this PR. They were taken with a copy of the example that seedsMath.random()and runscomputeStep()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.