@motion-core/motion-gpu
Version:
Framework-agnostic WebGPU runtime for fullscreen WGSL shaders with explicit Svelte, React, and Vue adapter entrypoints.
92 lines (91 loc) • 2.4 kB
JavaScript
import { assertComputeContract, extractWorkgroupSize } from "../core/compute-shader.js";
//#region src/lib/passes/ComputePass.ts
/**
* Compute pass class used within the render graph.
*
* Validates compute shader contract, parses workgroup size,
* and resolves dispatch dimensions. Does **not** manage GPU resources
* (that responsibility belongs to the renderer).
*/
var ComputePass = class {
/**
* Enables/disables this pass without removing it from graph.
*/
enabled;
/**
* Discriminant flag for render graph to identify compute passes.
*/
isCompute = true;
compute;
workgroupSize;
dispatch;
constructor(options) {
assertComputeContract(options.compute);
const workgroupSize = extractWorkgroupSize(options.compute);
this.compute = options.compute;
this.workgroupSize = workgroupSize;
this.dispatch = options.dispatch ?? "auto";
this.enabled = options.enabled ?? true;
}
/**
* Replaces current compute shader and updates workgroup size.
*
* @param compute - New compute shader WGSL source.
* @throws {Error} When shader does not match the compute contract.
*/
setCompute(compute) {
assertComputeContract(compute);
const workgroupSize = extractWorkgroupSize(compute);
this.compute = compute;
this.workgroupSize = workgroupSize;
}
/**
* Updates dispatch strategy.
*/
setDispatch(dispatch) {
this.dispatch = dispatch ?? "auto";
}
/**
* Returns current compute shader source.
*/
getCompute() {
return this.compute;
}
/**
* Returns parsed workgroup size from current compute shader.
*/
getWorkgroupSize() {
return [...this.workgroupSize];
}
/**
* Resolves dispatch workgroup counts for current frame.
*
* @param ctx - Dispatch context with canvas dimensions and timing.
* @returns Tuple [x, y, z] workgroup counts.
*/
resolveDispatch(ctx) {
if (this.dispatch === "auto") return [
Math.ceil(ctx.width / this.workgroupSize[0]),
Math.ceil(ctx.height / this.workgroupSize[1]),
Math.ceil(1 / this.workgroupSize[2])
];
if (typeof this.dispatch === "function") return this.dispatch(ctx);
if (Array.isArray(this.dispatch)) return [
this.dispatch[0],
this.dispatch[1] ?? 1,
this.dispatch[2] ?? 1
];
return [
1,
1,
1
];
}
/**
* Releases resources (no-op since GPU resource lifecycle is renderer-managed).
*/
dispose() {}
};
//#endregion
export { ComputePass };
//# sourceMappingURL=ComputePass.js.map