-
Notifications
You must be signed in to change notification settings - Fork 3.7k
GS WebGPU fastpath #18648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
CedricGuillemet
wants to merge
9
commits into
BabylonJS:master
Choose a base branch
from
CedricGuillemet:GSCSCulling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
GS WebGPU fastpath #18648
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5e09521
GS WebGPU fastpath
CedricGuillemet ddbccd1
Phase 1 with GPU sorting
CedricGuillemet 2dc5788
webgpu culling
CedricGuillemet 8e35713
fix build
CedricGuillemet 2fd440f
culling with static camera
CedricGuillemet bd9d758
revert
CedricGuillemet 13e2df7
feedback
CedricGuillemet 9544c4d
shader loading
CedricGuillemet 532adbe
release gc pressure
CedricGuillemet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import { type Nullable } from "core/types"; | ||
| import { type AbstractEngine } from "core/Engines/abstractEngine.pure"; | ||
| import { type WebGPUEngine } from "core/Engines/webgpuEngine.pure"; | ||
| import { type DataBuffer } from "core/Buffers/dataBuffer"; | ||
| import { type ComputeBindingMapping } from "core/Engines/Extensions/engine.computeShader.pure"; | ||
| import { ComputeShader } from "core/Compute/computeShader.pure"; | ||
| import { StorageBuffer } from "core/Buffers/storageBuffer"; | ||
| import { UniformBuffer } from "core/Materials/uniformBuffer"; | ||
| import { Constants } from "core/Engines/constants"; | ||
|
|
||
| const BlockSize = 256; | ||
|
|
||
| /** | ||
| * @internal | ||
| * WebGPU compute utility that performs an in-place hierarchical EXCLUSIVE prefix sum (scan) over a | ||
| * `StorageBuffer` of u32 values. | ||
| * | ||
| * It scans the array in blocks of {@link BlockSize}, recursively scans the per-block totals, and adds | ||
| * the resulting offsets back, so it handles arrays much larger than a single workgroup. It is used by | ||
| * the Gaussian Splatting GPU depth sort (histogram into per-bucket start offsets) and its interval | ||
| * culling pass. | ||
| * | ||
| * The WGSL compute shaders are loaded asynchronously during construction (they self-register into the | ||
| * ShaderStore), so this module stays side-effect free: no top-level shader imports and therefore no | ||
| * `.pure` / side-effect-wrapper split. {@link isReady} returns false until the shaders have loaded and | ||
| * the compute pipelines have compiled. | ||
| */ | ||
| export class PrefixSumCompute { | ||
| private readonly _engine: AbstractEngine; | ||
| private _scanBlock: Nullable<ComputeShader> = null; | ||
| private _addOffsets: Nullable<ComputeShader> = null; | ||
| // One UBO per dispatch, cycled per scan, to avoid a later dispatch's parameters overwriting an | ||
| // earlier (still-pending) dispatch's uniform buffer (the update/dispatch hazard). | ||
| private _ubos: UniformBuffer[] = []; | ||
| private _uboIndex = 0; | ||
| // Per-recursion-level scratch buffers holding the block totals; grown/cached by level. | ||
| private _levelBuffers: StorageBuffer[] = []; | ||
| // Set once the WGSL shader modules have been dynamically imported (and thus registered in the ShaderStore). | ||
| private _shadersLoaded = false; | ||
|
|
||
| /** | ||
| * Creates a new prefix-sum compute helper and kicks off asynchronous loading of its WGSL shaders. | ||
| * @param engine the (WebGPU) engine to run the compute passes on | ||
| */ | ||
| public constructor(engine: AbstractEngine) { | ||
| this._engine = engine; | ||
| // Async-load the compute shaders during construction. Importing the generated shader modules registers | ||
| // them into the ShaderStore as a side effect; isReady() stays false until this resolves. | ||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| this._loadShadersAsync(); | ||
| } | ||
|
|
||
| private async _loadShadersAsync(): Promise<void> { | ||
| await Promise.all([import("../ShadersWGSL/prefixSumScanBlock.compute"), import("../ShadersWGSL/prefixSumAddOffsets.compute")]); | ||
| this._shadersLoaded = true; | ||
| } | ||
|
|
||
| private _ensureShaders(): void { | ||
| if (this._scanBlock || !this._shadersLoaded) { | ||
| return; | ||
| } | ||
| const scanBlockBindings: ComputeBindingMapping = { | ||
| data: { group: 0, binding: 0 }, | ||
| blockSums: { group: 0, binding: 1 }, | ||
| params: { group: 0, binding: 2 }, | ||
| }; | ||
| this._scanBlock = new ComputeShader("prefixSumScanBlock", this._engine, "prefixSumScanBlock", { bindingsMapping: scanBlockBindings }); | ||
| const addOffsetsBindings: ComputeBindingMapping = { | ||
| data: { group: 0, binding: 0 }, | ||
| blockOffsets: { group: 0, binding: 1 }, | ||
| params: { group: 0, binding: 2 }, | ||
| }; | ||
| this._addOffsets = new ComputeShader("prefixSumAddOffsets", this._engine, "prefixSumAddOffsets", { bindingsMapping: addOffsetsBindings }); | ||
| } | ||
|
|
||
| private _getUbo(count: number): UniformBuffer { | ||
| if (this._uboIndex >= this._ubos.length) { | ||
| const ubo = new UniformBuffer(this._engine, undefined, undefined, "PrefixSumComputeParams", false, false); | ||
| ubo.addUniform("count", 1); | ||
| this._ubos.push(ubo); | ||
| } | ||
| const ubo = this._ubos[this._uboIndex++]; | ||
| ubo.updateUInt("count", count); | ||
| ubo.update(); | ||
| return ubo; | ||
| } | ||
|
|
||
| private _getLevelBuffer(level: number, numBlocks: number): StorageBuffer { | ||
| // Padded to a multiple of BlockSize so the next level's block scan never reads out of bounds. | ||
| const capacity = (Math.ceil(numBlocks / BlockSize) * BlockSize) | 0; | ||
| const existing = this._levelBuffers[level]; | ||
| if (existing && existing.getBuffer().capacity >= capacity * Uint32Array.BYTES_PER_ELEMENT) { | ||
| return existing; | ||
| } | ||
| existing?.dispose(); | ||
| const buffer = new StorageBuffer( | ||
| this._engine as WebGPUEngine, | ||
| Math.max(capacity, BlockSize) * Uint32Array.BYTES_PER_ELEMENT, | ||
| Constants.BUFFER_CREATIONFLAG_READWRITE, | ||
| "PrefixSumLevel" + level | ||
| ); | ||
| this._levelBuffers[level] = buffer; | ||
| return buffer; | ||
| } | ||
|
|
||
| /** | ||
| * Whether both compute shaders are loaded, compiled and ready to dispatch. | ||
| * @returns true when the scan can run | ||
| */ | ||
| public isReady(): boolean { | ||
| if (!this._shadersLoaded) { | ||
| return false; | ||
| } | ||
| this._ensureShaders(); | ||
| return !!this._scanBlock!.isReady() && !!this._addOffsets!.isReady(); | ||
| } | ||
|
|
||
| /** | ||
| * Runs an in-place exclusive prefix sum over the first `count` entries of `buffer`. | ||
| * Call {@link resetForFrame} once before the sequence of scans issued in a frame. | ||
| * @param buffer the u32 storage buffer to scan in place | ||
| * @param count number of valid entries to scan | ||
| */ | ||
| public scanExclusive(buffer: StorageBuffer, count: number): void { | ||
| this._ensureShaders(); | ||
| this._scanRecursive(buffer.getBuffer(), count, 0); | ||
| } | ||
|
|
||
| /** | ||
| * Resets the internal UBO ring. Must be called once at the start of each frame's scan sequence. | ||
| */ | ||
| public resetForFrame(): void { | ||
| this._uboIndex = 0; | ||
| } | ||
|
|
||
| private _scanRecursive(data: DataBuffer, count: number, level: number): void { | ||
| // count === 1 must still run the block scan so the single element is written to its exclusive value (0); | ||
| // returning here would leave it unchanged. Only an empty scan is a true no-op. | ||
| if (count <= 0) { | ||
| return; | ||
| } | ||
| const numBlocks = Math.ceil(count / BlockSize); | ||
| const blockSums = this._getLevelBuffer(level, numBlocks); | ||
|
|
||
| const scanUbo = this._getUbo(count); | ||
| this._scanBlock!.setStorageBuffer("data", data); | ||
| this._scanBlock!.setStorageBuffer("blockSums", blockSums); | ||
| this._scanBlock!.setUniformBuffer("params", scanUbo); | ||
| this._scanBlock!.dispatch(numBlocks); | ||
|
|
||
| if (numBlocks > 1) { | ||
| this._scanRecursive(blockSums.getBuffer(), numBlocks, level + 1); | ||
|
|
||
| const addUbo = this._getUbo(count); | ||
| this._addOffsets!.setStorageBuffer("data", data); | ||
| this._addOffsets!.setStorageBuffer("blockOffsets", blockSums); | ||
| this._addOffsets!.setUniformBuffer("params", addUbo); | ||
| this._addOffsets!.dispatch(numBlocks); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Releases all GPU resources held by the helper. | ||
| */ | ||
| public dispose(): void { | ||
| for (const ubo of this._ubos) { | ||
| ubo.dispose(); | ||
| } | ||
| for (const buffer of this._levelBuffers) { | ||
| buffer.dispose(); | ||
| } | ||
| this._ubos = []; | ||
| this._levelBuffers = []; | ||
| this._uboIndex = 0; | ||
| this._scanBlock = null; | ||
| this._addOffsets = null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.