Skip to content

Code-first video & image, rendered natively on WebGPU.

refactor: remove duplicated code across packages and nodes

okanasl24cb6079f52f
diff --git a/apps/renderer-service/src/cli.ts b/apps/renderer-service/src/cli.ts
index 8ff1c3c7..25c41ee9 100644
--- a/apps/renderer-service/src/cli.ts
+++ b/apps/renderer-service/src/cli.ts
@@ -10,6 +10,7 @@ import {
} from "@framefields/server-utils";
import { registerBackendServices } from "./di-setup.js";
import { RENDERER_ENV_CONFIG } from "./env-config.js";
+import { renderAndUpload } from "./render-upload.js";
async function main() {
const jobArgIndex = process.argv.indexOf("--job");
@@ -55,66 +56,27 @@ async function main() {
"[CLI] Starting render process...",
);
- const renderer = new HeadlessMediaRenderer();
- let buffer: Buffer | undefined;
- let filePath: string | undefined;
- let contentType: string;
- let fileCleanup: (() => Promise<void>) | undefined;
let probedMeta: any;
- if (normalizedType === "video" || normalizedType === "gif") {
- const renderResult = await renderer.renderVideo(virtualMedia, {
- codec,
- audioCodec,
- quality,
- });
- filePath = renderResult.filePath;
- fileCleanup = renderResult.cleanup;
- contentType =
- codec === "vp8" || codec === "vp9"
- ? "video/webm"
- : codec === "gif"
- ? "image/gif"
- : codec === "mp3"
- ? "audio/mpeg"
- : codec === "aac"
- ? "audio/mp4"
- : codec === "opus"
- ? "audio/webm"
- : "video/mp4";
- } else if (normalizedType === "lut") {
- buffer = await renderer.renderLut(virtualMedia, frame);
- contentType = "text/plain";
- } else {
- buffer = await renderer.renderImage(virtualMedia, frame);
- contentType = "image/png";
- }
+ const contentType = await renderAndUpload({
+ virtualMedia,
+ type,
+ frame,
+ codec,
+ audioCodec,
+ quality,
+ key,
+ bucket,
+ logPrefix: "[CLI]",
+ });
const storage = container.get<StorageService>(TOKENS.STORAGE);
- if (filePath) {
- rendererLogger.info(
- { filePath },
- "[CLI] Render complete - uploading file to storage.",
- );
- await storage.uploadFileToStorage(filePath, key, contentType, bucket);
- } else if (buffer) {
- rendererLogger.info(
- { fileSize: buffer.length },
- "[CLI] Render complete - uploading buffer to storage.",
- );
- await storage.uploadToStorage(buffer, key, contentType, bucket);
- }
-
const publicUrl = storage.getPublicUrl?.(key, bucket);
rendererLogger.info(
{ key, bucket, contentType },
"[CLI] Render complete and uploaded to R2 successfully.",
);
- if (fileCleanup) {
- await fileCleanup().catch(() => {});
- }
-
// Print the final result in JSON format as stdout for the wrapper script to read
console.log(
JSON.stringify({
diff --git a/apps/renderer-service/src/job-processor.ts b/apps/renderer-service/src/job-processor.ts
index 23ce4ca6..8a53d08d 100644
--- a/apps/renderer-service/src/job-processor.ts
+++ b/apps/renderer-service/src/job-processor.ts
@@ -1,11 +1,6 @@
import type { VirtualMediaData } from "@framefields/core";
-import { HeadlessMediaRenderer } from "@framefields/renderer";
-import {
- container,
- rendererLogger,
- type StorageService,
- TOKENS,
-} from "@framefields/server-utils";
+import { rendererLogger } from "@framefields/server-utils";
+import { renderAndUpload } from "./render-upload.js";
export interface RenderJobInput {
virtualMedia?: VirtualMediaData;
@@ -61,11 +56,6 @@ export async function executeRenderJob(
quality,
} = jobData;
- const renderer = new HeadlessMediaRenderer();
- let buffer: Buffer | undefined;
- let filePath: string | undefined;
- let contentType: string;
- let fileCleanup: (() => Promise<void>) | undefined;
let probedMeta: any;
const normalizedType = type.toLowerCase();
@@ -84,52 +74,17 @@ export async function executeRenderJob(
);
}
- if (normalizedType === "video" || normalizedType === "gif") {
- const renderResult = await renderer.renderVideo(virtualMedia, {
- codec,
- audioCodec,
- quality,
- });
- filePath = renderResult.filePath;
- fileCleanup = renderResult.cleanup;
- contentType =
- codec === "vp8" || codec === "vp9"
- ? "video/webm"
- : codec === "gif"
- ? "image/gif"
- : codec === "mp3"
- ? "audio/mpeg"
- : codec === "aac"
- ? "audio/mp4"
- : codec === "opus"
- ? "audio/webm"
- : "video/mp4";
- } else if (normalizedType === "lut") {
- buffer = await renderer.renderLut(virtualMedia, frame);
- contentType = "text/plain";
- } else {
- buffer = await renderer.renderImage(virtualMedia, frame);
- contentType = "image/png";
- }
-
- const storage = container.get<StorageService>(TOKENS.STORAGE);
- if (filePath) {
- rendererLogger.info(
- { filePath },
- "[HTTP Local] Render complete - uploading file to storage.",
- );
- await storage.uploadFileToStorage(filePath, key, contentType, bucket);
- } else if (buffer) {
- rendererLogger.info(
- { fileSize: buffer.length },
- "[HTTP Local] Render complete - uploading buffer to storage.",
- );
- await storage.uploadToStorage(buffer, key, contentType, bucket);
- }
-
- if (fileCleanup) {
- await fileCleanup().catch(() => {});
- }
+ await renderAndUpload({
+ virtualMedia,
+ type,
+ frame,
+ codec,
+ audioCodec,
+ quality,
+ key,
+ bucket,
+ logPrefix: "[HTTP Local]",
+ });
job.status = "COMPLETED";
job.output = {
diff --git a/apps/renderer-service/src/render-upload.ts b/apps/renderer-service/src/render-upload.ts
new file mode 100644
index 00000000..8d1da807
--- /dev/null
+++ b/apps/renderer-service/src/render-upload.ts
@@ -0,0 +1,89 @@
+import type { VirtualMediaData } from "@framefields/core";
+import { HeadlessMediaRenderer } from "@framefields/renderer";
+import {
+ container,
+ rendererLogger,
+ type StorageService,
+ TOKENS,
+} from "@framefields/server-utils";
+
+export interface RenderUploadOptions {
+ virtualMedia: VirtualMediaData;
+ /** "video" / "gif" render a file, "lut" a LUT, anything else a PNG frame. */
+ type: string;
+ frame?: number;
+ codec?: string;
+ audioCodec?: string;
+ quality?: string;
+ key: string;
+ bucket: string;
+ /** Log line prefix, e.g. "[CLI]". */
+ logPrefix: string;
+}
+
+function videoContentType(codec: string | undefined): string {
+ return codec === "vp8" || codec === "vp9"
+ ? "video/webm"
+ : codec === "gif"
+ ? "image/gif"
+ : codec === "mp3"
+ ? "audio/mpeg"
+ : codec === "aac"
+ ? "audio/mp4"
+ : codec === "opus"
+ ? "audio/webm"
+ : "video/mp4";
+}
+
+/**
+ * Renders `virtualMedia` and uploads the result to storage under `key`.
+ * Returns the uploaded content type.
+ */
+export async function renderAndUpload(
+ options: RenderUploadOptions,
+): Promise<string> {
+ const { virtualMedia, frame, key, bucket, logPrefix } = options;
+ const normalizedType = options.type.toLowerCase();
+ const renderer = new HeadlessMediaRenderer();
+ let buffer: Buffer | undefined;
+ let filePath: string | undefined;
+ let contentType: string;
+ let fileCleanup: (() => Promise<void>) | undefined;
+
+ if (normalizedType === "video" || normalizedType === "gif") {
+ const renderResult = await renderer.renderVideo(virtualMedia, {
+ codec: options.codec,
+ audioCodec: options.audioCodec,
+ quality: options.quality,
+ });
+ filePath = renderResult.filePath;
+ fileCleanup = renderResult.cleanup;
+ contentType = videoContentType(options.codec);
+ } else if (normalizedType === "lut") {
+ buffer = await renderer.renderLut(virtualMedia, frame);
+ contentType = "text/plain";
+ } else {
+ buffer = await renderer.renderImage(virtualMedia, frame);
+ contentType = "image/png";
+ }
+
+ const storage = container.get<StorageService>(TOKENS.STORAGE);
+ if (filePath) {
+ rendererLogger.info(
+ { filePath },
+ `${logPrefix} Render complete - uploading file to storage.`,
+ );
+ await storage.uploadFileToStorage(filePath, key, contentType, bucket);
+ } else if (buffer) {
+ rendererLogger.info(
+ { fileSize: buffer.length },
+ `${logPrefix} Render complete - uploading buffer to storage.`,
+ );
+ await storage.uploadToStorage(buffer, key, contentType, bucket);
+ }
+
+ if (fileCleanup) {
+ await fileCleanup().catch(() => {});
+ }
+ return contentType;
+}
diff --git a/nodes/node-apply-lut/src/renderers/webgpu-renderer.ts b/nodes/node-apply-lut/src/renderers/webgpu-renderer.ts
index ad9b27b1..42f05973 100644
--- a/nodes/node-apply-lut/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-apply-lut/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,15 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTiming,
+} from "@framefields/node-sdk/gpu";
import { lutStore, signalRegistry } from "@framefields/webgpu-renderers";
export const LUT_3D_SHADER = `
@@ -17,27 +27,7 @@ struct LutUniforms {
@group(2) @binding(1) var lutSamp : sampler;
@group(3) @binding(0) var signalTex : texture_2d<f32>;
@group(3) @binding(1) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let inputColor = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -71,27 +61,7 @@ struct LutUniforms {
@group(2) @binding(1) var lutSamp : sampler;
@group(3) @binding(0) var signalTex : texture_2d<f32>;
@group(3) @binding(1) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let inputColor = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -114,99 +84,22 @@ struct VSOut {
}
`;
-interface DeviceLutLayouts {
- lutUniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- lutTextureLayout3D: GPUBindGroupLayout;
- lutTextureLayout1D: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceLutLayouts>();
const lutUniformData = new Float32Array(4);
-function getDeviceLayouts(device: GPUDevice): DeviceLutLayouts {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const lutUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
- const lutTextureLayout3D = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { viewDimension: "3d", sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
- const lutTextureLayout1D = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { viewDimension: "1d", sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- lutUniformLayout,
- singleTextureLayout,
- lutTextureLayout3D,
- lutTextureLayout1D,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ lutUniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ lutTextureLayout3D: fragmentBindGroupLayout(device, [
+ { texture: { viewDimension: "3d", sampleType: "float" } },
+ "sampler",
+ ]),
+ lutTextureLayout1D: fragmentBindGroupLayout(device, [
+ { texture: { viewDimension: "1d", sampleType: "float" } },
+ "sampler",
+ ]),
+ signalTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getLutResources(
device: GPUDevice,
@@ -224,23 +117,16 @@ function getLutResources(
});
const currentLutLayout =
type === "3D" ? layouts.lutTextureLayout3D : layouts.lutTextureLayout1D;
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `LutPipeline_${type}_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.lutUniformLayout,
- layouts.singleTextureLayout,
- currentLutLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module: lutModule, entryPoint: "vs" },
- fragment: {
- module: lutModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: lutModule,
+ layouts: [
+ layouts.lutUniformLayout,
+ layouts.singleTextureLayout,
+ currentLutLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -260,12 +146,10 @@ export const LutWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
const fps = props.fps || 30;
@@ -360,55 +244,11 @@ export const LutWebGPURenderer: WebGPUNodeRenderer = async (args) => {
}
}
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- // Clear temporary texture
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- // Draw child frame into tmpTex
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// If no LUT is ready (and no fallback is available), draw the child frame directly as a passthrough
if (!lut) {
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- tmpTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
- args.pass = finalPass;
+ drawTextureToTarget(args, tmpTex, op.opacity ?? 1);
return;
}
@@ -433,13 +273,7 @@ export const LutWebGPURenderer: WebGPUNodeRenderer = async (args) => {
// Fetch dynamic intensity signal texture view
let signalView: GPUTextureView;
if (hasStaticSignal) {
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
signalView = signalRegistry.getOrCreate2DTextureView(
ctx.device,
encoder,
@@ -465,36 +299,17 @@ export const LutWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const uniformBuffer = ctx.renderer.getTemporaryBuffer(lutUniformData);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline!);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline!, [
ctx.device.createBindGroup({
layout: uLayout!,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout!,
tmpTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: lLayout!,
entries: [
@@ -502,9 +317,6 @@ export const LutWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 1, resource: sampler },
],
}),
- );
- renderPass.setBindGroup(
- 3,
ctx.device.createBindGroup({
layout: sigLayout!,
entries: [
@@ -512,25 +324,8 @@ export const LutWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 1, resource: sampler },
],
}),
- );
- renderPass.draw(4);
- renderPass.end();
+ ]);
// Blending/opacity draw to the targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-audio-compressor/src/renderers/audio-processor.ts b/nodes/node-audio-compressor/src/renderers/audio-processor.ts
index cf5cf38b..a08dfa98 100644
--- a/nodes/node-audio-compressor/src/renderers/audio-processor.ts
+++ b/nodes/node-audio-compressor/src/renderers/audio-processor.ts
@@ -1,4 +1,8 @@
-import { type AudioProcessor, resolveNumber } from "@framefields/node-sdk";
+import {
+ type AudioProcessor,
+ isHandleConnected,
+ resolveNumber,
+} from "@framefields/node-sdk";
import { WebGPUAudioProcessor } from "@framefields/webgpu-renderers";
const PARAM_ORDER = [
@@ -211,11 +215,6 @@ export const compressorAudioProcessor: AudioProcessor = async (
}
const op = (virtualMedia.operation as Record<string, unknown>) || {};
- const inputs =
- (op.inputs as Record<
- string,
- { connectionValid?: boolean; outputItem?: { type?: string } }
- >) || {};
const threshold = resolveNumber(op.threshold, -24);
const ratio = resolveNumber(op.ratio, 4);
@@ -224,24 +223,16 @@ export const compressorAudioProcessor: AudioProcessor = async (
const knee = resolveNumber(op.knee, 6);
const makeupGain = resolveNumber(op.makeupGain, 0);
- const isHandleConnected = (handleIdKey: string): boolean => {
- const handleId = op[handleIdKey];
- if (typeof handleId !== "string" || !handleId) return false;
- const input = inputs[handleId];
- return Boolean(
- input?.connectionValid &&
- input.outputItem &&
- (input.outputItem.type === "Signal" ||
- input.outputItem.type === "Number"),
- );
- };
-
- const hasThresholdSig = isHandleConnected("thresholdHandleId") ? 1.0 : 0.0;
- const hasRatioSig = isHandleConnected("ratioHandleId") ? 1.0 : 0.0;
- const hasAttackSig = isHandleConnected("attackHandleId") ? 1.0 : 0.0;
- const hasReleaseSig = isHandleConnected("releaseHandleId") ? 1.0 : 0.0;
- const hasKneeSig = isHandleConnected("kneeHandleId") ? 1.0 : 0.0;
- const hasMakeupGainSig = isHandleConnected("makeupGainHandleId") ? 1.0 : 0.0;
+ const hasThresholdSig = isHandleConnected(op, "thresholdHandleId")
+ ? 1.0
+ : 0.0;
+ const hasRatioSig = isHandleConnected(op, "ratioHandleId") ? 1.0 : 0.0;
+ const hasAttackSig = isHandleConnected(op, "attackHandleId") ? 1.0 : 0.0;
+ const hasReleaseSig = isHandleConnected(op, "releaseHandleId") ? 1.0 : 0.0;
+ const hasKneeSig = isHandleConnected(op, "kneeHandleId") ? 1.0 : 0.0;
+ const hasMakeupGainSig = isHandleConnected(op, "makeupGainHandleId")
+ ? 1.0
+ : 0.0;
const numChannels = channels.length;
if (numChannels === 0) return;
diff --git a/nodes/node-audio-delay/src/renderers/audio-processor.ts b/nodes/node-audio-delay/src/renderers/audio-processor.ts
index 8342e935..6cdd9b7e 100644
--- a/nodes/node-audio-delay/src/renderers/audio-processor.ts
+++ b/nodes/node-audio-delay/src/renderers/audio-processor.ts
@@ -1,5 +1,6 @@
import {
type AudioProcessor,
+ isHandleConnected,
resolveBoolean,
resolveNumber,
} from "@framefields/node-sdk";
@@ -183,11 +184,6 @@ export const delayAudioProcessor: AudioProcessor = async (
}
const op = (virtualMedia.operation as Record<string, unknown>) || {};
- const inputs =
- (op.inputs as Record<
- string,
- { connectionValid?: boolean; outputItem?: { type?: string } }
- >) || {};
const numChannels = channels.length;
if (numChannels === 0) return;
@@ -207,22 +203,12 @@ export const delayAudioProcessor: AudioProcessor = async (
const dryInternal = Math.max(0.0, Math.min(resolveNumber(op.dry, 1.0), 1.0));
const pingPongInternal = resolveBoolean(op.pingPong, false);
- const isHandleConnected = (handleIdKey: string): boolean => {
- const handleId = op[handleIdKey];
- if (typeof handleId !== "string" || !handleId) return false;
- const input = inputs[handleId];
- return Boolean(
- input?.connectionValid &&
- input.outputItem &&
- (input.outputItem.type === "Signal" ||
- input.outputItem.type === "Number"),
- );
- };
-
- const hasDelayTimeSig = isHandleConnected("delayTimeHandleId") ? 1.0 : 0.0;
- const hasFeedbackSig = isHandleConnected("feedbackHandleId") ? 1.0 : 0.0;
- const hasWetSig = isHandleConnected("wetHandleId") ? 1.0 : 0.0;
- const hasDrySig = isHandleConnected("dryHandleId") ? 1.0 : 0.0;
+ const hasDelayTimeSig = isHandleConnected(op, "delayTimeHandleId")
+ ? 1.0
+ : 0.0;
+ const hasFeedbackSig = isHandleConnected(op, "feedbackHandleId") ? 1.0 : 0.0;
+ const hasWetSig = isHandleConnected(op, "wetHandleId") ? 1.0 : 0.0;
+ const hasDrySig = isHandleConnected(op, "dryHandleId") ? 1.0 : 0.0;
// Allocate max possible delay buffer (5 seconds) to allow smooth dynamic modulation
const maxDelaySec = 5.0;
diff --git a/nodes/node-audio-fade/src/renderers/audio-processor.ts b/nodes/node-audio-fade/src/renderers/audio-processor.ts
index 06984b91..9e1ccee3 100644
--- a/nodes/node-audio-fade/src/renderers/audio-processor.ts
+++ b/nodes/node-audio-fade/src/renderers/audio-processor.ts
@@ -1,5 +1,6 @@
import {
type AudioProcessor,
+ isHandleConnected,
resolveNumber,
resolveString,
} from "@framefields/node-sdk";
@@ -137,11 +138,6 @@ export const fadeAudioProcessor: AudioProcessor = async (
}
const op = (virtualMedia.operation as Record<string, unknown>) || {};
- const inputs =
- (op.inputs as Record<
- string,
- { connectionValid?: boolean; outputItem?: { type?: string } }
- >) || {};
const numChannels = channels.length;
if (numChannels === 0) return;
@@ -175,22 +171,10 @@ export const fadeAudioProcessor: AudioProcessor = async (
fadeOut = Math.min(fadeOut, half);
}
- const isHandleConnected = (handleIdKey: string): boolean => {
- const handleId = op[handleIdKey];
- if (typeof handleId !== "string" || !handleId) return false;
- const input = inputs[handleId];
- return Boolean(
- input?.connectionValid &&
- input.outputItem &&
- (input.outputItem.type === "Signal" ||
- input.outputItem.type === "Number"),
- );
- };
-
- const hasFadeInDurationSig = isHandleConnected("fadeInDurationHandleId")
+ const hasFadeInDurationSig = isHandleConnected(op, "fadeInDurationHandleId")
? 1.0
: 0.0;
- const hasFadeOutDurationSig = isHandleConnected("fadeOutDurationHandleId")
+ const hasFadeOutDurationSig = isHandleConnected(op, "fadeOutDurationHandleId")
? 1.0
: 0.0;
diff --git a/nodes/node-audio-noise-gate/src/renderers/audio-processor.ts b/nodes/node-audio-noise-gate/src/renderers/audio-processor.ts
index 2c872380..84f7442b 100644
--- a/nodes/node-audio-noise-gate/src/renderers/audio-processor.ts
+++ b/nodes/node-audio-noise-gate/src/renderers/audio-processor.ts
@@ -1,4 +1,8 @@
-import { type AudioProcessor, resolveNumber } from "@framefields/node-sdk";
+import {
+ type AudioProcessor,
+ isHandleConnected,
+ resolveNumber,
+} from "@framefields/node-sdk";
import { WebGPUAudioProcessor } from "@framefields/webgpu-renderers";
const PARAM_ORDER = ["threshold", "attack", "hold", "release", "range"];
@@ -238,11 +242,6 @@ export const noiseGateAudioProcessor: AudioProcessor = async (
}
const op = (virtualMedia.operation as Record<string, unknown>) || {};
- const inputs =
- (op.inputs as Record<
- string,
- { connectionValid?: boolean; outputItem?: { type?: string } }
- >) || {};
const threshold = resolveNumber(op.threshold, -40);
const attack = resolveNumber(op.attack, 0.005);
@@ -252,23 +251,13 @@ export const noiseGateAudioProcessor: AudioProcessor = async (
const closedGain = range <= -96 ? 0 : 10 ** (range / 20);
- const isHandleConnected = (handleIdKey: string): boolean => {
- const handleId = op[handleIdKey];
- if (typeof handleId !== "string" || !handleId) return false;
- const input = inputs[handleId];
- return Boolean(
- input?.connectionValid &&
- input.outputItem &&
- (input.outputItem.type === "Signal" ||
- input.outputItem.type === "Number"),
- );
- };
-
- const hasThresholdSig = isHandleConnected("thresholdHandleId") ? 1.0 : 0.0;
- const hasAttackSig = isHandleConnected("attackHandleId") ? 1.0 : 0.0;
- const hasHoldSig = isHandleConnected("holdHandleId") ? 1.0 : 0.0;
- const hasReleaseSig = isHandleConnected("releaseHandleId") ? 1.0 : 0.0;
- const hasRangeSig = isHandleConnected("rangeHandleId") ? 1.0 : 0.0;
+ const hasThresholdSig = isHandleConnected(op, "thresholdHandleId")
+ ? 1.0
+ : 0.0;
+ const hasAttackSig = isHandleConnected(op, "attackHandleId") ? 1.0 : 0.0;
+ const hasHoldSig = isHandleConnected(op, "holdHandleId") ? 1.0 : 0.0;
+ const hasReleaseSig = isHandleConnected(op, "releaseHandleId") ? 1.0 : 0.0;
+ const hasRangeSig = isHandleConnected(op, "rangeHandleId") ? 1.0 : 0.0;
const numChannels = channels.length;
if (numChannels === 0) return;
diff --git a/nodes/node-audio-parametric-eq/src/renderers/audio-processor.ts b/nodes/node-audio-parametric-eq/src/renderers/audio-processor.ts
index cf67ca08..595db11b 100644
--- a/nodes/node-audio-parametric-eq/src/renderers/audio-processor.ts
+++ b/nodes/node-audio-parametric-eq/src/renderers/audio-processor.ts
@@ -1,5 +1,6 @@
import {
type AudioProcessor,
+ isHandleConnected,
resolveNumber,
resolveString,
} from "@framefields/node-sdk";
@@ -221,11 +222,6 @@ export const parametricEqAudioProcessor: AudioProcessor = async (
}
const op = (virtualMedia.operation as Record<string, unknown>) || {};
- const inputs =
- (op.inputs as Record<
- string,
- { connectionValid?: boolean; outputItem?: { type?: string } }
- >) || {};
const rawType = resolveString(op.type, "peak");
const config: ParametricEqNodeConfig = {
@@ -248,21 +244,11 @@ export const parametricEqAudioProcessor: AudioProcessor = async (
qHandleId: typeof op.qHandleId === "string" ? op.qHandleId : null,
};
- const isHandleConnected = (handleIdKey: string): boolean => {
- const handleId = op[handleIdKey];
- if (typeof handleId !== "string" || !handleId) return false;
- const input = inputs[handleId];
- return Boolean(
- input?.connectionValid &&
- input.outputItem &&
- (input.outputItem.type === "Signal" ||
- input.outputItem.type === "Number"),
- );
- };
-
- const hasFrequencySig = isHandleConnected("frequencyHandleId") ? 1.0 : 0.0;
- const hasGainSig = isHandleConnected("gainHandleId") ? 1.0 : 0.0;
- const hasQSig = isHandleConnected("qHandleId") ? 1.0 : 0.0;
+ const hasFrequencySig = isHandleConnected(op, "frequencyHandleId")
+ ? 1.0
+ : 0.0;
+ const hasGainSig = isHandleConnected(op, "gainHandleId") ? 1.0 : 0.0;
+ const hasQSig = isHandleConnected(op, "qHandleId") ? 1.0 : 0.0;
const numChannels = channels.length;
if (numChannels === 0) return;
diff --git a/nodes/node-audio-reverb/src/renderers/audio-processor.ts b/nodes/node-audio-reverb/src/renderers/audio-processor.ts
index c417fd50..87bc55b0 100644
--- a/nodes/node-audio-reverb/src/renderers/audio-processor.ts
+++ b/nodes/node-audio-reverb/src/renderers/audio-processor.ts
@@ -1,4 +1,8 @@
-import { type AudioProcessor, resolveNumber } from "@framefields/node-sdk";
+import {
+ type AudioProcessor,
+ isHandleConnected,
+ resolveNumber,
+} from "@framefields/node-sdk";
import { WebGPUAudioProcessor } from "@framefields/webgpu-renderers";
const PARAM_ORDER = ["roomSize", "damping", "wet", "dry", "width"];
@@ -289,11 +293,6 @@ export const reverbAudioProcessor: AudioProcessor = async (
}
const op = (virtualMedia.operation as Record<string, unknown>) || {};
- const inputs =
- (op.inputs as Record<
- string,
- { connectionValid?: boolean; outputItem?: { type?: string } }
- >) || {};
const numChannels = channels.length;
if (numChannels === 0) return;
@@ -316,23 +315,11 @@ export const reverbAudioProcessor: AudioProcessor = async (
Math.min(resolveNumber(op.width, 1.0), 1.0),
);
- const isHandleConnected = (handleIdKey: string): boolean => {
- const handleId = op[handleIdKey];
- if (typeof handleId !== "string" || !handleId) return false;
- const input = inputs[handleId];
- return Boolean(
- input?.connectionValid &&
- input.outputItem &&
- (input.outputItem.type === "Signal" ||
- input.outputItem.type === "Number"),
- );
- };
-
- const hasRoomSizeSig = isHandleConnected("roomSizeHandleId") ? 1.0 : 0.0;
- const hasDampingSig = isHandleConnected("dampingHandleId") ? 1.0 : 0.0;
- const hasWetSig = isHandleConnected("wetHandleId") ? 1.0 : 0.0;
- const hasDrySig = isHandleConnected("dryHandleId") ? 1.0 : 0.0;
- const hasWidthSig = isHandleConnected("widthHandleId") ? 1.0 : 0.0;
+ const hasRoomSizeSig = isHandleConnected(op, "roomSizeHandleId") ? 1.0 : 0.0;
+ const hasDampingSig = isHandleConnected(op, "dampingHandleId") ? 1.0 : 0.0;
+ const hasWetSig = isHandleConnected(op, "wetHandleId") ? 1.0 : 0.0;
+ const hasDrySig = isHandleConnected(op, "dryHandleId") ? 1.0 : 0.0;
+ const hasWidthSig = isHandleConnected(op, "widthHandleId") ? 1.0 : 0.0;
const scale = sampleRate / 44100;
diff --git a/nodes/node-blur/src/index.ts b/nodes/node-blur/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-blur/src/index.ts
+++ b/nodes/node-blur/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-blur/src/renderers/webgpu-renderer.ts b/nodes/node-blur/src/renderers/webgpu-renderer.ts
index 4a3c5345..5ef7cfae 100644
--- a/nodes/node-blur/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-blur/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,21 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ unwrapFrameParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
import { MAX_BLUR } from "../shared/config.js";
@@ -125,27 +140,7 @@ fn applyPartialBlur(blurred: vec4<f32>, uv: vec2<f32>, dimensions: vec2<f32>) ->
export const GAUSSIAN_BLUR_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let c = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -184,27 +179,7 @@ struct VSOut {
export const BOX_BLUR_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let c = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -238,32 +213,12 @@ struct VSOut {
export const MEDIAN_BLUR_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
struct Val {
luma : f32,
color : vec4<f32>,
};
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let centerCol = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -316,27 +271,7 @@ struct Val {
export const MOTION_BLUR_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let c = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -373,27 +308,7 @@ struct VSOut {
export const BILATERAL_BLUR_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let centerCol = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -445,27 +360,7 @@ struct VSOut {
export const EDGE_PRESERVING_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let centerCol = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -544,27 +439,7 @@ struct VSOut {
export const RADIAL_BLUR_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let centerCol = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -612,27 +487,7 @@ struct VSOut {
export const ZOOM_BLUR_SHADER = `
${WGSL_BLUR_UNIFORMS}
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let centerCol = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -672,93 +527,16 @@ struct VSOut {
}
`;
-interface DeviceBlurResources {
- blurUniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceBlurResources>();
-
-function getDeviceLayouts(device: GPUDevice): DeviceBlurResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const blurUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- blurUniformLayout,
- singleTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ blurUniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ "texture",
+ ]),
+ signalTextureLayout: texturesLayout(device, 5),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getBlurResources(
device: GPUDevice,
@@ -792,22 +570,15 @@ function getBlurResources(
code,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `BlurPipeline_${blurType}_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.blurUniformLayout,
- layouts.singleTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module: blurModule, entryPoint: "vs" },
- fragment: {
- module: blurModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: blurModule,
+ layouts: [
+ layouts.blurUniformLayout,
+ layouts.singleTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -825,12 +596,10 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
const compositionFrame = props.compositionFrame ?? frame;
@@ -895,36 +664,16 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
val = Math.max(minVal, Math.min(maxVal, rawVal));
if (scaleByDpi) val *= dpiScale;
} else {
- let rawVal = op[configKey] as unknown;
- if (rawVal !== undefined && rawVal !== null) {
- if (typeof rawVal === "object") {
- if (
- "get" in (rawVal as Record<string, unknown>) &&
- typeof (rawVal as Record<string, unknown>).get === "function"
- ) {
- rawVal = (
- rawVal as {
- get: (ctx: { frame: number; fps: number }) => unknown;
- }
- ).get({ frame: compositionFrame, fps });
- } else if ("value" in (rawVal as Record<string, unknown>)) {
- const sig = rawVal as {
- value: unknown;
- gpuBinding?: { textureView?: GPUTextureView };
- };
- if (sig.gpuBinding?.textureView) {
- gpuTextureView = sig.gpuBinding.textureView;
- hasStaticSig = true;
- }
- rawVal = sig.value;
- }
- } else if (typeof rawVal === "function") {
- rawVal = (rawVal as (f: number, fps: number) => unknown)(
- compositionFrame,
- fps,
- );
- }
+ const unwrapped = unwrapFrameParam(
+ op[configKey],
+ compositionFrame,
+ fps,
+ );
+ if (unwrapped.gpuTextureView) {
+ gpuTextureView = unwrapped.gpuTextureView;
+ hasStaticSig = true;
}
+ const rawVal = unwrapped.value;
const numVal = Number(rawVal ?? defaultValue);
val = Math.max(
minVal,
@@ -949,35 +698,7 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const radiusRes = resolveBindable("radius", 0.3, 0.001, 1.0, false);
const radiusYRes = resolveBindable("radiusY", 0.0, 0.0, 1.0, false);
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- // Clear intermediate texture
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
const blurType = op.blurType ?? "Gaussian";
const isSeparable =
@@ -997,13 +718,7 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
// Fetch dynamic signal texture views on-demand
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
// Pre-extract audio features if any connected bindable signals use audio sources
for (const bindableRes of [
@@ -1023,6 +738,15 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
}
}
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -1031,27 +755,7 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
gpuTextureView?: GPUTextureView;
},
suffix: string,
- ) => {
- if (res.gpuTextureView) {
- return res.gpuTextureView;
- }
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const strengthView = getSignalView(strengthRes, "strength_sig");
const sigmaColorView = getSignalView(sigmaColorRes, "sigmacolor_sig");
@@ -1114,27 +818,12 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
blurData[8] = 0.0;
const hBuffer = ctx.renderer.getTemporaryBuffer(blurData);
- const hPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex1.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- hPass.setPipeline(pipeline);
- hPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex1.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: hBuffer } }],
}),
- );
- hPass.setBindGroup(1, createBlurBindGroup1(tmpTex, tmpTex));
- hPass.setBindGroup(
- 2,
+ createBlurBindGroup1(tmpTex, tmpTex),
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -1146,9 +835,7 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 5, resource: sampler },
],
}),
- );
- hPass.draw(4);
- hPass.end();
+ ]);
// Pass 2: Vertical (applies partialBlur mask with original tmpTex)
blurData[0] = 0.0;
@@ -1156,27 +843,12 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
blurData[8] = isPartial ? 1.0 : 0.0;
const vBuffer = ctx.renderer.getTemporaryBuffer(blurData);
- const vPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex2.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- vPass.setPipeline(pipeline);
- vPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex2.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: vBuffer } }],
}),
- );
- vPass.setBindGroup(1, createBlurBindGroup1(outTex1, tmpTex));
- vPass.setBindGroup(
- 2,
+ createBlurBindGroup1(outTex1, tmpTex),
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -1188,9 +860,7 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 5, resource: sampler },
],
}),
- );
- vPass.draw(4);
- vPass.end();
+ ]);
} else {
// 1-Pass Filters (Motion, Edge-preserving, Radial, Zoom)
outTex2 = ctx.renderer.getTemporaryTexture(width, height, [
@@ -1204,27 +874,12 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
blurData[8] = isPartial ? 1.0 : 0.0;
const singleBuffer = ctx.renderer.getTemporaryBuffer(blurData);
- const singlePass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex2.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- singlePass.setPipeline(pipeline);
- singlePass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex2.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: singleBuffer } }],
}),
- );
- singlePass.setBindGroup(1, createBlurBindGroup1(tmpTex, tmpTex));
- singlePass.setBindGroup(
- 2,
+ createBlurBindGroup1(tmpTex, tmpTex),
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -1236,26 +891,9 @@ export const BlurWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 5, resource: sampler },
],
}),
- );
- singlePass.draw(4);
- singlePass.end();
+ ]);
}
// Final Pass: Draw result to targetView using standard drawTexture for blending/opacity
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex2,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex2, op.opacity ?? 1);
};
diff --git a/nodes/node-camera-parallax-3d/src/index.ts b/nodes/node-camera-parallax-3d/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-camera-parallax-3d/src/index.ts
+++ b/nodes/node-camera-parallax-3d/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-camera-parallax-3d/src/renderers/webgpu-renderer.ts b/nodes/node-camera-parallax-3d/src/renderers/webgpu-renderer.ts
index 659dbfe7..4a2095c1 100644
--- a/nodes/node-camera-parallax-3d/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-camera-parallax-3d/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,19 @@
/// <reference types="webgpu" />
import type { SignalData, VirtualMediaData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ resolveSignalField,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
interface CameraParallax3DOp {
@@ -103,27 +116,7 @@ struct CameraParallaxUniforms {
@group(3) @binding(6) var fovSigTex : texture_2d<f32>;
@group(3) @binding(7) var edgeDilationSigTex : texture_2d<f32>;
@group(3) @binding(8) var sigSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn sampleDepth(uv: vec2<f32>) -> f32 {
let clampedUV = clamp(uv, vec2<f32>(0.0), vec2<f32>(1.0));
@@ -344,123 +337,13 @@ fn sampleAlbedo(uv: vec2<f32>) -> vec4<f32> {
}
`;
-interface DeviceCameraParallaxResources {
- uniformLayout: GPUBindGroupLayout;
- srcTextureLayout: GPUBindGroupLayout;
- depthTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<
- GPUDevice,
- DeviceCameraParallaxResources
->();
-
-function getDeviceLayouts(device: GPUDevice): DeviceCameraParallaxResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const srcTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const depthTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 7,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 8,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- srcTextureLayout,
- depthTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ srcTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ depthTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: texturesLayout(device, 8),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getCameraParallaxResources(
device: GPUDevice,
@@ -476,23 +359,16 @@ function getCameraParallaxResources(
code: CAMERA_PARALLAX_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `CameraParallaxPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.srcTextureLayout,
- layouts.depthTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.srcTextureLayout,
+ layouts.depthTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
@@ -507,55 +383,6 @@ function getCameraParallaxResources(
};
}
-function resolveSignalField(
- op: CameraParallax3DOp,
- fieldName:
- | "panX"
- | "panY"
- | "dollyZ"
- | "fov"
- | "parallaxAmount"
- | "dofAperture"
- | "focusPlane"
- | "edgeDilation",
- defaultValue: number,
- minVal: number,
- maxVal: number,
-): { hasSignal: boolean; sd: CameraParallaxSignalData | null; value: number } {
- const handleIdKey = `${fieldName}HandleId` as keyof CameraParallax3DOp;
- const handleId = op[handleIdKey] as string | null | undefined;
- const signalInput = handleId ? op.inputs?.[handleId] : null;
-
- const hasSignal = !!(
- signalInput?.connectionValid &&
- (signalInput.outputItem?.type === "Signal" ||
- signalInput.outputItem?.type === "Numeric")
- );
- const sd =
- hasSignal && signalInput?.outputItem?.data
- ? (signalInput.outputItem.data as CameraParallaxSignalData)
- : null;
-
- let value = defaultValue;
- if (!hasSignal) {
- if (
- signalInput?.connectionValid &&
- signalInput.outputItem?.type === "Number"
- ) {
- const raw = Number(signalInput.outputItem.data ?? defaultValue);
- value = Math.max(minVal, Math.min(maxVal, raw));
- } else {
- const raw = Number(op[fieldName] ?? defaultValue);
- value = Math.max(minVal, Math.min(maxVal, raw));
- }
- } else if (sd) {
- const raw = Number(sd.offset ?? defaultValue);
- value = Math.max(minVal, Math.min(maxVal, raw));
- }
-
- return { hasSignal, sd, value };
-}
-
export const CameraParallax3DWebGPURenderer: WebGPUNodeRenderer = async (
args,
) => {
@@ -563,12 +390,10 @@ export const CameraParallax3DWebGPURenderer: WebGPUNodeRenderer = async (
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -587,39 +412,14 @@ export const CameraParallax3DWebGPURenderer: WebGPUNodeRenderer = async (
const height = targetHeight;
// 1. Draw input source child to temporary texture
- const srcTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const srcView = srcTex.createView();
-
- const srcClearPass = ctx.renderer.beginFrame(
- encoder,
- srcView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- srcClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- sourceMedia,
- {
+ const { texture: srcTex } = await drawChildToTexture(args, sourceMedia, {
+ props: {
...props,
virtualMedia: sourceMedia,
containerWidth: width,
containerHeight: height,
},
- srcView,
- srcTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// End active pass from child drawing
args.pass.end();
@@ -627,51 +427,24 @@ export const CameraParallax3DWebGPURenderer: WebGPUNodeRenderer = async (
// 2. Draw depth map if provided
const depthMedia = op.depthMedia;
let depthTex: GPUTexture;
- let depthView: GPUTextureView;
let hasDepthMap = false;
if (depthMedia) {
- depthTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- srcTex,
- ]);
- depthView = depthTex.createView();
-
- const depthClearPass = ctx.renderer.beginFrame(
- encoder,
- depthView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- depthClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- depthMedia,
- {
+ ({ texture: depthTex } = await drawChildToTexture(args, depthMedia, {
+ props: {
...props,
virtualMedia: depthMedia,
containerWidth: width,
containerHeight: height,
},
- depthView,
- depthTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ exclude: [srcTex],
+ }));
// End active pass from depth drawing
args.pass.end();
hasDepthMap = true;
} else {
depthTex = srcTex;
- depthView = srcView;
hasDepthMap = false;
}
@@ -702,14 +475,56 @@ export const CameraParallax3DWebGPURenderer: WebGPUNodeRenderer = async (
? (elapsedSeconds % durationSeconds) / durationSeconds
: 0.0;
- const panXInfo = resolveSignalField(op, "panX", 0.0, -1.0, 1.0);
- const panYInfo = resolveSignalField(op, "panY", 0.0, -1.0, 1.0);
- const dollyZInfo = resolveSignalField(op, "dollyZ", 0.0, -1.0, 1.0);
- const fovInfo = resolveSignalField(op, "fov", 50.0, 15.0, 120.0);
- const parallaxInfo = resolveSignalField(op, "parallaxAmount", 0.6, 0.0, 2.0);
- const apertureInfo = resolveSignalField(op, "dofAperture", 0.3, 0.0, 1.0);
- const focusPlaneInfo = resolveSignalField(op, "focusPlane", 0.4, 0.0, 1.0);
- const edgeDilationInfo = resolveSignalField(
+ const panXInfo = resolveSignalField<CameraParallaxSignalData>(
+ op,
+ "panX",
+ 0.0,
+ -1.0,
+ 1.0,
+ );
+ const panYInfo = resolveSignalField<CameraParallaxSignalData>(
+ op,
+ "panY",
+ 0.0,
+ -1.0,
+ 1.0,
+ );
+ const dollyZInfo = resolveSignalField<CameraParallaxSignalData>(
+ op,
+ "dollyZ",
+ 0.0,
+ -1.0,
+ 1.0,
+ );
+ const fovInfo = resolveSignalField<CameraParallaxSignalData>(
+ op,
+ "fov",
+ 50.0,
+ 15.0,
+ 120.0,
+ );
+ const parallaxInfo = resolveSignalField<CameraParallaxSignalData>(
+ op,
+ "parallaxAmount",
+ 0.6,
+ 0.0,
+ 2.0,
+ );
+ const apertureInfo = resolveSignalField<CameraParallaxSignalData>(
+ op,
+ "dofAperture",
+ 0.3,
+ 0.0,
+ 1.0,
+ );
+ const focusPlaneInfo = resolveSignalField<CameraParallaxSignalData>(
+ op,
+ "focusPlane",
+ 0.4,
+ 0.0,
+ 1.0,
+ );
+ const edgeDilationInfo = resolveSignalField<CameraParallaxSignalData>(
op,
"edgeDilation",
0.5,
@@ -813,45 +628,23 @@ export const CameraParallax3DWebGPURenderer: WebGPUNodeRenderer = async (
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
srcLayout,
srcTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 2,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
depthLayout,
depthTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 3,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -866,26 +659,8 @@ export const CameraParallax3DWebGPURenderer: WebGPUNodeRenderer = async (
{ binding: 8, resource: sampler },
],
}),
- );
-
- renderPass.draw(4);
- renderPass.end();
+ ]);
// 7. Final Pass to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-canvas-generator/src/renderers/webgpu-renderer.ts b/nodes/node-canvas-generator/src/renderers/webgpu-renderer.ts
index 462b95ff..070d835a 100644
--- a/nodes/node-canvas-generator/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-canvas-generator/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,11 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+} from "@framefields/node-sdk/gpu";
import type { CanvasGeneratorNodeConfig } from "../shared/config.js";
const CANVAS_SHADER = `
@@ -16,27 +22,7 @@ struct CanvasUniforms {
};
@group(0) @binding(0) var<uniform> u : CanvasUniforms;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
if (u.fillType < 0.5) {
@@ -58,35 +44,12 @@ struct VSOut {
}
`;
-interface DeviceCanvasResources {
- uniformLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceCanvasResources>();
const canvasUniformData = new Float32Array(20);
-function getDeviceLayouts(device: GPUDevice): DeviceCanvasResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getCanvasResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -99,18 +62,11 @@ function getCanvasResources(device: GPUDevice, format: GPUTextureFormat) {
code: CANVAS_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `CanvasGeneratorPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [layouts.uniformLayout],
- }),
- vertex: { module: canvasModule, entryPoint: "vs" },
- fragment: {
- module: canvasModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: canvasModule,
+ layouts: [layouts.uniformLayout],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
diff --git a/nodes/node-channel-merger/src/renderers/webgpu-renderer.ts b/nodes/node-channel-merger/src/renderers/webgpu-renderer.ts
index c6f49d41..37540095 100644
--- a/nodes/node-channel-merger/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-channel-merger/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,12 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ beginTargetPass,
+ drawChildToTexture,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import type { ChannelColorSpace, ChannelMergerOp } from "../shared/index.js";
const COLOR_SPACE_MAP: Record<ChannelColorSpace, number> = {
@@ -23,27 +30,7 @@ struct MergerUniforms {
@group(1) @binding(2) var tex3 : texture_2d<f32>;
@group(1) @binding(3) var tex4 : texture_2d<f32>;
@group(1) @binding(4) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn hue2rgb(p: f32, q: f32, t_in: f32) -> f32 {
var t = t_in;
@@ -147,45 +134,9 @@ function getDeviceLayouts(device: GPUDevice): MergerDeviceResources {
let res = deviceResourceCache.get(device);
if (res) return res;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = texturesLayout(device, 4);
res = {
uniformLayout,
@@ -232,7 +183,6 @@ export const ChannelMergerWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
@@ -257,111 +207,49 @@ export const ChannelMergerWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const height = targetHeight;
// 1. Draw Channel 1
- const tex1 = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const view1 = tex1.createView();
- const pass1 = ctx.renderer.beginFrame(
- encoder,
- view1,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- pass1.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ const { texture: tex1, view: view1 } = await drawChildToTexture(
+ args,
ch1Media,
{
- ...props,
- virtualMedia: ch1Media,
- renderId: `${props.renderId}-ch1`,
- excludeTextures: [...(props.excludeTextures || []), tex1],
+ props: {
+ ...props,
+ virtualMedia: ch1Media,
+ renderId: `${props.renderId}-ch1`,
+ },
},
- view1,
- tex1,
- width,
- height,
);
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
args.pass.end();
// 2. Draw Channel 2
- const tex2 = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- tex1,
- ]);
- const view2 = tex2.createView();
- const pass2 = ctx.renderer.beginFrame(
- encoder,
- view2,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- pass2.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ const { texture: tex2, view: view2 } = await drawChildToTexture(
+ args,
ch2Media,
{
- ...props,
- virtualMedia: ch2Media,
- renderId: `${props.renderId}-ch2`,
- excludeTextures: [...(props.excludeTextures || []), tex1, tex2],
+ props: {
+ ...props,
+ virtualMedia: ch2Media,
+ renderId: `${props.renderId}-ch2`,
+ excludeTextures: [...(props.excludeTextures || []), tex1],
+ },
+ exclude: [tex1],
},
- view2,
- tex2,
- width,
- height,
);
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
args.pass.end();
// 3. Draw Channel 3
- const tex3 = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- tex1,
- tex2,
- ]);
- const view3 = tex3.createView();
- const pass3 = ctx.renderer.beginFrame(
- encoder,
- view3,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- pass3.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ const { texture: tex3, view: view3 } = await drawChildToTexture(
+ args,
ch3Media,
{
- ...props,
- virtualMedia: ch3Media,
- renderId: `${props.renderId}-ch3`,
- excludeTextures: [...(props.excludeTextures || []), tex1, tex2, tex3],
+ props: {
+ ...props,
+ virtualMedia: ch3Media,
+ renderId: `${props.renderId}-ch3`,
+ excludeTextures: [...(props.excludeTextures || []), tex1, tex2],
+ },
+ exclude: [tex1, tex2],
},
- view3,
- tex3,
- width,
- height,
);
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
args.pass.end();
// 4. Draw Channel 4 (if connected)
@@ -441,19 +329,10 @@ export const ChannelMergerWebGPURenderer: WebGPUNodeRenderer = async (args) => {
],
});
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "load",
- );
+ const finalPass = beginTargetPass(args);
finalPass.setPipeline(pipeline);
finalPass.setBindGroup(0, uniformBindGroup);
finalPass.setBindGroup(1, textureBindGroup);
finalPass.draw(4, 1, 0, 0);
-
- args.pass = finalPass;
};
diff --git a/nodes/node-channel-splitter/src/renderers/webgpu-renderer.ts b/nodes/node-channel-splitter/src/renderers/webgpu-renderer.ts
index 307f6482..9d845043 100644
--- a/nodes/node-channel-splitter/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-channel-splitter/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,11 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ beginTargetPass,
+ drawChildToTexture,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
import type { ChannelColorSpace, ChannelSplitterOp } from "../shared/index.js";
const COLOR_SPACE_MAP: Record<ChannelColorSpace, number> = {
@@ -20,27 +26,7 @@ struct SplitterUniforms {
@group(0) @binding(0) var<uniform> u : SplitterUniforms;
@group(1) @binding(0) var srcTex : texture_2d<f32>;
@group(1) @binding(1) var srcSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn srgb2lin(v: f32) -> f32 {
let cv = clamp(v, 0.0, 1.0);
@@ -143,30 +129,9 @@ function getDeviceLayouts(device: GPUDevice): SplitterDeviceResources {
let res = deviceResourceCache.get(device);
if (res) return res;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, ["texture", "sampler"]);
res = {
uniformLayout,
@@ -211,17 +176,7 @@ function getPipeline(
export const ChannelSplitterWebGPURenderer: WebGPUNodeRenderer = async (
args,
) => {
- const {
- ctx,
- encoder,
- pass,
- targetView,
- targetTexture,
- targetWidth,
- targetHeight,
- props,
- drawChild,
- } = args;
+ const { ctx, pass, targetTexture, props } = args;
const op = props.virtualMedia?.operation as ChannelSplitterOp | undefined;
if (op?.op !== "ChannelSplitter" || !op) return;
@@ -232,43 +187,14 @@ export const ChannelSplitterWebGPURenderer: WebGPUNodeRenderer = async (
// End previous pass
pass.end();
- const width = targetWidth;
- const height = targetHeight;
-
// Draw source child into temp texture
- const srcTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const srcView = srcTex.createView();
-
- const srcClearPass = ctx.renderer.beginFrame(
- encoder,
- srcView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- srcClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- sourceMedia,
- {
+ const { view: srcView } = await drawChildToTexture(args, sourceMedia, {
+ props: {
...props,
virtualMedia: sourceMedia,
renderId: `${props.renderId}-src`,
- excludeTextures: [...(props.excludeTextures || []), srcTex],
},
- srcView,
- srcTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// Close pass opened by drawChild
args.pass.end();
@@ -300,19 +226,10 @@ export const ChannelSplitterWebGPURenderer: WebGPUNodeRenderer = async (
],
});
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "load",
- );
+ const finalPass = beginTargetPass(args);
finalPass.setPipeline(pipeline);
finalPass.setBindGroup(0, uniformBindGroup);
finalPass.setBindGroup(1, textureBindGroup);
finalPass.draw(4, 1, 0, 0);
-
- args.pass = finalPass;
};
diff --git a/nodes/node-color-balance/src/index.ts b/nodes/node-color-balance/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-color-balance/src/index.ts
+++ b/nodes/node-color-balance/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-color-balance/src/renderers/webgpu-renderer.ts b/nodes/node-color-balance/src/renderers/webgpu-renderer.ts
index 249ec621..07acac21 100644
--- a/nodes/node-color-balance/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-color-balance/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,19 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ resolveHandleParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTextureView,
+ signalTiming,
+} from "@framefields/node-sdk/gpu";
import type { ColorBalanceOperation } from "../shared/config.js";
type ColorBalanceSignalData = SignalData & { nodeId?: string };
@@ -66,27 +78,7 @@ struct ColorBalanceUniforms {
@group(2) @binding(7) var highlightsMagentaGreenSigTex: texture_2d<f32>;
@group(2) @binding(8) var highlightsYellowBlueSigTex: texture_2d<f32>;
@group(2) @binding(9) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn get_luminance(c: vec3<f32>) -> f32 {
return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
@@ -173,102 +165,45 @@ function getColorBalanceResources(
let res = deviceResourceCache.get(device);
if (res) return res;
- const uniformLayout = device.createBindGroupLayout({
- label: "ColorBalanceUniformLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(
+ device,
+ ["uniform"],
+ "ColorBalanceUniformLayout",
+ );
- const textureLayout = device.createBindGroupLayout({
- label: "ColorBalanceTextureLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(
+ device,
+ ["texture", "sampler"],
+ "ColorBalanceTextureLayout",
+ );
- const signalTextureLayout = device.createBindGroupLayout({
- label: "ColorBalanceSignalTextureLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 7,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 8,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 9,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
+ const signalTextureLayout = fragmentBindGroupLayout(
+ device,
+ [
+ "texture",
+ "texture",
+ "texture",
+ "texture",
+ "texture",
+ "texture",
+ "texture",
+ "texture",
+ "texture",
+ "sampler",
],
- });
+ "ColorBalanceSignalTextureLayout",
+ );
const module = device.createShaderModule({
label: "ColorBalanceShaderModule",
code: WGSL_COLOR_BALANCE_SHADER,
});
- const pipeline = device.createRenderPipeline({
+ const pipeline = createFullscreenPipeline(device, {
label: "ColorBalancePipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [uniformLayout, textureLayout, signalTextureLayout],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: { module, entryPoint: "fs", targets: [{ format }] },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [uniformLayout, textureLayout, signalTextureLayout],
+ format,
});
res = {
@@ -288,12 +223,10 @@ export const ColorBalanceWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as unknown as
@@ -321,38 +254,14 @@ export const ColorBalanceWebGPURenderer: WebGPUNodeRenderer = async (args) => {
fallbackVal: number,
clampMin = -100,
clampMax = 100,
- ): {
- val: number;
- hasStaticSig: boolean;
- sd: ColorBalanceSignalData | null;
- } => {
- if (handleId && op.inputs?.[handleId]?.connectionValid) {
- const outputItem = op.inputs[handleId].outputItem;
- if (outputItem?.type === "Number") {
- const numVal =
- typeof outputItem.data === "number"
- ? outputItem.data
- : ((outputItem.data as { value?: number })?.value ?? fallbackVal);
- return {
- val: Math.max(clampMin, Math.min(clampMax, numVal)),
- hasStaticSig: false,
- sd: null,
- };
- }
- if (outputItem?.type === "Signal" && outputItem.data) {
- return {
- val: fallbackVal,
- hasStaticSig: true,
- sd: outputItem.data as ColorBalanceSignalData,
- };
- }
- }
- return {
- val: Math.max(clampMin, Math.min(clampMax, fallbackVal)),
- hasStaticSig: false,
- sd: null,
- };
- };
+ ) =>
+ resolveHandleParam<ColorBalanceSignalData>(
+ op.inputs,
+ handleId,
+ fallbackVal,
+ clampMin,
+ clampMax,
+ );
const resolveVal = (val: unknown): number => {
if (val !== undefined && val !== null) {
@@ -417,51 +326,24 @@ export const ColorBalanceWebGPURenderer: WebGPUNodeRenderer = async (args) => {
resolveVal(op.highlights?.yellowBlue),
);
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
- ...props,
- virtualMedia: childMedia,
- },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
const { pipeline, uniformLayout, textureLayout, signalTextureLayout } =
getColorBalanceResources(ctx.device, ctx.renderer.format);
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -469,24 +351,7 @@ export const ColorBalanceWebGPURenderer: WebGPUNodeRenderer = async (args) => {
sd: ColorBalanceSignalData | null;
},
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const sCRView = getSignalView(sCR, "shadows_cyanRed_sig");
const sMGView = getSignalView(sMG, "shadows_magentaGreen_sig");
@@ -555,53 +420,19 @@ export const ColorBalanceWebGPURenderer: WebGPUNodeRenderer = async (args) => {
],
});
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [{ binding: 0, resource: { buffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
textureLayout,
tmpTex,
sampler,
),
- );
- renderPass.setBindGroup(2, sigBindGroup);
- renderPass.draw(4);
- renderPass.end();
-
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: props.opacity ?? 1 },
- );
+ sigBindGroup,
+ ]);
- (args as unknown as { pass: GPURenderPassEncoder }).pass = finalPass;
+ drawTextureToTarget(args, outTex, props.opacity ?? 1);
};
diff --git a/nodes/node-colorkey/src/renderers/webgpu-renderer.ts b/nodes/node-colorkey/src/renderers/webgpu-renderer.ts
index d3e744be..0604693d 100644
--- a/nodes/node-colorkey/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-colorkey/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,17 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
interface ColorKeyOp {
@@ -52,27 +63,7 @@ struct ColorKeyUniforms {
@group(2) @binding(1) var smoothnessSigTex : texture_2d<f32>;
@group(2) @binding(2) var spillSigTex : texture_2d<f32>;
@group(2) @binding(3) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn rgb2yuv(rgb: vec3<f32>) -> vec3<f32> {
let y = 0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b;
@@ -168,79 +159,14 @@ fn yuv2rgb(yuv: vec3<f32>) -> vec3<f32> {
}
`;
-interface DeviceColorKeyResources {
- uniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceColorKeyResources>();
const colorKeyData = new Float32Array(12);
-function getDeviceLayouts(device: GPUDevice): DeviceColorKeyResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- singleTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: texturesLayout(device, 3),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getColorKeyResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -253,22 +179,15 @@ function getColorKeyResources(device: GPUDevice, format: GPUTextureFormat) {
code: COLORKEY_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `ColorKeyPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.singleTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module: colorKeyModule, entryPoint: "vs" },
- fragment: {
- module: colorKeyModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: colorKeyModule,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.singleTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -300,12 +219,10 @@ export const ColorKeyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
const fps = props.fps || 30;
@@ -424,35 +341,7 @@ export const ColorKeyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
hasStaticSpillSig = true;
}
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- // Clear intermediate texture
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// Fetch static resources and compile pipeline once
const {
@@ -465,13 +354,7 @@ export const ColorKeyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
// Fetch dynamic signal texture views on-demand
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
let similarityView: GPUTextureView;
if (hasStaticSimilaritySig && similaritySd) {
@@ -560,35 +443,17 @@ export const ColorKeyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const singleBuffer = ctx.renderer.getTemporaryBuffer(colorKeyData);
- const singlePass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- singlePass.setPipeline(pipeline);
- singlePass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: singleBuffer } }],
}),
- );
- singlePass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout,
tmpTex,
sampler,
),
- );
- singlePass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -598,25 +463,8 @@ export const ColorKeyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 3, resource: sampler },
],
}),
- );
- singlePass.draw(4);
- singlePass.end();
+ ]);
// Final Pass: Draw result to targetView using standard drawTexture for blending/opacity
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-compositor/src/renderers/transform3d.test.ts b/nodes/node-compositor/src/renderers/transform3d.test.ts
index 27c400f8..4f78c9af 100644
--- a/nodes/node-compositor/src/renderers/transform3d.test.ts
+++ b/nodes/node-compositor/src/renderers/transform3d.test.ts
@@ -1,3 +1,4 @@
+import { solveHomography } from "@framefields/node-sdk";
import { describe, expect, it } from "vitest";
import {
type CompilerVirtualMedia,
@@ -6,7 +7,6 @@ import {
import {
has3DTransform,
type Point2D,
- solveHomography,
Transform3DMath,
} from "./transform3d.js";
diff --git a/nodes/node-compositor/src/renderers/transform3d.ts b/nodes/node-compositor/src/renderers/transform3d.ts
index 8aae691a..426ad597 100644
--- a/nodes/node-compositor/src/renderers/transform3d.ts
+++ b/nodes/node-compositor/src/renderers/transform3d.ts
@@ -285,89 +285,6 @@ export const Transform3DMath = {
},
};
-/**
- * Computes an 8-parameter 3x3 homography matrix mapping destination coordinates (quad)
- * to normalized source coordinates (unit square [0, 1]^2).
- *
- * The homography H maps destination (x, y) to source (u, v):
- * u = (h00*x + h01*y + h02) / (h20*x + h21*y + h22)
- * v = (h10*x + h11*y + h12) / (h20*x + h21*y + h22)
- *
- * Resulting array: [h00, h01, h02, h10, h11, h12, h20, h21, 1.0]
- */
-export function solveHomography(
- dstPoints: Point2D[],
- srcPoints: Point2D[] = [
- { x: 0.0, y: 0.0 }, // TL
- { x: 1.0, y: 0.0 }, // TR
- { x: 0.0, y: 1.0 }, // BL
- { x: 1.0, y: 1.0 }, // BR
- ],
-): number[] {
- const A: number[][] = [];
- const B: number[] = [];
-
- for (let i = 0; i < 4; i++) {
- const xi = dstPoints[i]?.x ?? 0;
- const yi = dstPoints[i]?.y ?? 0;
- const ui = srcPoints[i]?.x ?? 0;
- const vi = srcPoints[i]?.y ?? 0;
-
- A.push([xi, yi, 1.0, 0.0, 0.0, 0.0, -xi * ui, -yi * ui]);
- B.push(ui);
-
- A.push([0.0, 0.0, 0.0, xi, yi, 1.0, -xi * vi, -yi * vi]);
- B.push(vi);
- }
-
- const n = 8;
- for (let i = 0; i < n; i++) {
- // Find pivot row
- let maxRow = i;
- let maxVal = Math.abs(A[i][i]);
- for (let k = i + 1; k < n; k++) {
- if (Math.abs(A[k][i]) > maxVal) {
- maxVal = Math.abs(A[k][i]);
- maxRow = k;
- }
- }
-
- // Swap rows in A and B
- const tempRow = A[i];
- A[i] = A[maxRow];
- A[maxRow] = tempRow;
-
- const tempB = B[i];
- B[i] = B[maxRow];
- B[maxRow] = tempB;
-
- const pivot = A[i][i];
- if (Math.abs(pivot) < 1e-12) {
- A[i][i] = pivot < 0 ? -1e-12 : 1e-12;
- }
-
- for (let k = i + 1; k < n; k++) {
- const factor = A[k][i] / A[i][i];
- for (let j = i; j < n; j++) {
- A[k][j] -= factor * A[i][j];
- }
- B[k] -= factor * B[i];
- }
- }
-
- // Back-substitution
- const h = new Array(8).fill(0.0);
- for (let i = n - 1; i >= 0; i--) {
- let sum = B[i];
- for (let j = i + 1; j < n; j++) {
- sum -= A[i][j] * h[j];
- }
- h[i] = sum / A[i][i];
- }
-
- return [h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], 1.0];
-}
-
export interface Transform3DPropsLike {
rotateX?: number;
rotateY?: number;
diff --git a/nodes/node-compositor/src/renderers/webgpu-renderer.ts b/nodes/node-compositor/src/renderers/webgpu-renderer.ts
index 1ce50a64..11a71316 100644
--- a/nodes/node-compositor/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-compositor/src/renderers/webgpu-renderer.ts
@@ -1,4 +1,5 @@
/// <reference types="webgpu" />
+
import { resolveMediaSourceUrl } from "@framefields/client-utils";
import { decodeAudioSource } from "@framefields/compositions";
import {
@@ -14,7 +15,17 @@ import {
getActiveMediaMetadata,
type VirtualMediaData,
} from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ resolveFrameNumber,
+ solveHomography,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ PREMULTIPLIED_BLEND,
+} from "@framefields/node-sdk/gpu";
import { measureText } from "@framefields/renderers";
import { TemporalDeflickerPipeline } from "@framefields/tensor-webgpu";
import {
@@ -53,17 +64,11 @@ import {
srtLoader,
Vector3Math,
} from "@framefields/webgpu-renderers";
-
import { compileTimeline } from "../shared/compiler.js";
import type { CompositorOperation } from "../shared/config.js";
import { shouldPaintContainerBackground } from "./container-bg.js";
import { buildLayerMatrix, toDOMMatrix } from "./transform.js";
-import {
- has3DTransform,
- type Quad2D,
- solveHomography,
- Transform3DMath,
-} from "./transform3d.js";
+import { has3DTransform, type Quad2D, Transform3DMath } from "./transform3d.js";
// Layout depends on the document tree (whose `children` array keeps its
// identity between frames), the viewport, media sizes and the few layout
@@ -109,27 +114,7 @@ struct HomographyUniforms {
@group(0) @binding(0) var<uniform> u : HomographyUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let x = in.uv.x;
@@ -169,69 +154,24 @@ function getHomographyResources(
): DeviceHomographyResources {
let res = deviceHomographyCache.get(device);
if (!res) {
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
const module = device.createShaderModule({
label: "compositor_homography.wgsl",
code: compositorHomographyWgsl,
});
- const pipeline = device.createRenderPipeline({
+ const pipeline = createFullscreenPipeline(device, {
label: "CompositorHomographyPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [uniformLayout, textureLayout],
- }),
- vertex: {
- module,
- entryPoint: "vs",
- },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ module,
+ layouts: [uniformLayout, textureLayout],
+ format,
+ blend: PREMULTIPLIED_BLEND,
});
res = { pipeline, uniformLayout, textureLayout };
@@ -302,52 +242,6 @@ function getTemporalDeflickerPipeline(
return pipe;
}
-function resolveVal(
- val: unknown,
- fallback: number,
- frame: number,
- fps: number,
-): number {
- if (val === undefined || val === null) return fallback;
- if (typeof val === "number") return Number.isFinite(val) ? val : fallback;
- if (typeof val === "object" && val !== null) {
- if (
- "get" in (val as Record<string, unknown>) &&
- typeof (val as { get: unknown }).get === "function"
- ) {
- try {
- const res = (val as { get: (ctx?: unknown) => unknown }).get({
- frame,
- fps,
- time: fps > 0 ? frame / fps : 0,
- duration: 0,
- durationMs: 0,
- progress: 0,
- deltaTime: fps > 0 ? 1 / fps : 0,
- });
- const n = Number(res);
- if (Number.isFinite(n)) return n;
- } catch {
- // fallback
- }
- }
- if ("value" in (val as Record<string, unknown>)) {
- const n = Number((val as { value: unknown }).value);
- if (Number.isFinite(n)) return n;
- }
- }
- if (typeof val === "function") {
- try {
- const n = Number(val({ frame, fps, time: fps > 0 ? frame / fps : 0 }));
- if (Number.isFinite(n)) return n;
- } catch {
- // fallback
- }
- }
- const n = Number(val);
- return Number.isFinite(n) ? n : fallback;
-}
-
function synthesizeRelightLights(
relightOpts: Record<string, unknown>,
sceneLights: Light3DOptions[],
@@ -373,12 +267,27 @@ function synthesizeRelightLights(
: "point";
const rawIntensity = relightOpts.intensity ?? 1.0;
- const intensity = resolveVal(rawIntensity, 1.0, shiftedFrame, fps);
+ const intensity = resolveFrameNumber(rawIntensity, 1.0, shiftedFrame, fps);
- const normX = resolveVal(relightOpts.lightPosX, 0.5, shiftedFrame, fps);
- const normY = resolveVal(relightOpts.lightPosY, 0.5, shiftedFrame, fps);
- const normZ = resolveVal(relightOpts.lightPosZ, 0.25, shiftedFrame, fps);
- const normRadius = resolveVal(
+ const normX = resolveFrameNumber(
+ relightOpts.lightPosX,
+ 0.5,
+ shiftedFrame,
+ fps,
+ );
+ const normY = resolveFrameNumber(
+ relightOpts.lightPosY,
+ 0.5,
+ shiftedFrame,
+ fps,
+ );
+ const normZ = resolveFrameNumber(
+ relightOpts.lightPosZ,
+ 0.25,
+ shiftedFrame,
+ fps,
+ );
+ const normRadius = resolveFrameNumber(
relightOpts.lightRadius,
0.75,
shiftedFrame,
@@ -401,7 +310,7 @@ function synthesizeRelightLights(
decay: 1.0,
};
- const ambientInt = resolveVal(
+ const ambientInt = resolveFrameNumber(
relightOpts.ambientIntensity,
0.15,
shiftedFrame,
@@ -1452,11 +1361,12 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
activeExcludes = [...activeExcludes, coverage];
nodeIndex += 1;
if (mode !== "alpha") {
- const white = ctx.renderer.getTemporaryTexture(
- targetW,
- targetH,
- [targetTexture, activeTex, ...activeExcludes, ...pendingTextures()],
- );
+ const white = ctx.renderer.getTemporaryTexture(targetW, targetH, [
+ targetTexture,
+ activeTex,
+ ...activeExcludes,
+ ...pendingTextures(),
+ ]);
ctx.renderer
.beginFrame(
encoder,
@@ -1467,13 +1377,19 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
"clear",
)
.end();
- coverage = ctx.renderer.composite(encoder, white, coverage, blend, [
- targetTexture,
- activeTex,
- ...activeExcludes,
- ...pendingTextures(),
+ coverage = ctx.renderer.composite(
+ encoder,
white,
- ]);
+ coverage,
+ blend,
+ [
+ targetTexture,
+ activeTex,
+ ...activeExcludes,
+ ...pendingTextures(),
+ white,
+ ],
+ );
activeExcludes = [...activeExcludes, coverage];
}
const before = pending3DItems.length;
@@ -1581,10 +1497,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
> => {
// Textures queued for the pending 3D flush are still live: a later
// layer of the same size must not be handed one from the pool.
- const excludes = [
- ...baseExcludes,
- ...pendingTextures(),
- ];
+ const excludes = [...baseExcludes, ...pendingTextures()];
const lop = nodeVM.operation as any;
if (lop.kind === "camera" || lop.kind === "light") return;
const targetId = lop.id || lop.inputHandleId;
@@ -3043,7 +2956,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
viewportHeight: targetH,
layerX: 0,
layerY: 0,
- roughness: resolveVal(
+ roughness: resolveFrameNumber(
target?.roughness ??
target?.relightRoughness ??
relightOpts.specularRoughness ??
@@ -3052,7 +2965,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- specularStrength: resolveVal(
+ specularStrength: resolveFrameNumber(
target?.specularStrength ??
target?.relightSpecularStrength ??
relightOpts.specularStrength,
@@ -3060,7 +2973,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- metallic: resolveVal(
+ metallic: resolveFrameNumber(
target?.metallic ??
target?.relightMetallic ??
relightOpts.metallic,
@@ -3068,7 +2981,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- ambientIntensity: resolveVal(
+ ambientIntensity: resolveFrameNumber(
target?.ambientIntensity ??
target?.relightAmbientIntensity ??
relightOpts.ambientIntensity,
@@ -3076,7 +2989,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- depthScale: resolveVal(
+ depthScale: resolveFrameNumber(
target?.depthScale ??
target?.relightDepthScale ??
relightOpts.depthScale,
@@ -3694,7 +3607,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
viewportHeight: texH,
layerX: sampledX,
layerY: sampledY,
- roughness: resolveVal(
+ roughness: resolveFrameNumber(
target?.roughness ??
target?.relightRoughness ??
relightOpts.specularRoughness ??
@@ -3703,7 +3616,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- specularStrength: resolveVal(
+ specularStrength: resolveFrameNumber(
target?.specularStrength ??
target?.relightSpecularStrength ??
relightOpts.specularStrength,
@@ -3711,7 +3624,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- metallic: resolveVal(
+ metallic: resolveFrameNumber(
target?.metallic ??
target?.relightMetallic ??
relightOpts.metallic,
@@ -3719,7 +3632,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- ambientIntensity: resolveVal(
+ ambientIntensity: resolveFrameNumber(
target?.ambientIntensity ??
target?.relightAmbientIntensity ??
relightOpts.ambientIntensity,
@@ -3727,7 +3640,7 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
compositionFrame,
fps ?? 24,
),
- depthScale: resolveVal(
+ depthScale: resolveFrameNumber(
target?.depthScale ??
target?.relightDepthScale ??
relightOpts.depthScale,
diff --git a/nodes/node-corner-pin/src/index.ts b/nodes/node-corner-pin/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-corner-pin/src/index.ts
+++ b/nodes/node-corner-pin/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-corner-pin/src/renderers/webgpu-renderer.ts b/nodes/node-corner-pin/src/renderers/webgpu-renderer.ts
index e27a8674..dfa65fa4 100644
--- a/nodes/node-corner-pin/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-corner-pin/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,14 @@
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { solveHomography } from "../shared/utils.js";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ STRAIGHT_ALPHA_BLEND,
+} from "@framefields/node-sdk/gpu";
+import { solveCornerPinHomography } from "../shared/utils.js";
const cornerPinWgsl = `
struct CornerPinUniforms {
@@ -11,27 +20,7 @@ struct CornerPinUniforms {
@group(0) @binding(0) var<uniform> u : CornerPinUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let x = in.uv.x;
@@ -65,69 +54,24 @@ const deviceResourceCache = new WeakMap<GPUDevice, DeviceCornerPinResources>();
function getCornerPinResources(device: GPUDevice, format: GPUTextureFormat) {
let res = deviceResourceCache.get(device);
if (!res) {
- const pinUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const pinUniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const pinTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const pinTextureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
const module = device.createShaderModule({
label: "corner_pin.wgsl",
code: cornerPinWgsl,
});
- const pinPipeline = device.createRenderPipeline({
+ const pinPipeline = createFullscreenPipeline(device, {
label: "CornerPinPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [pinUniformLayout, pinTextureLayout],
- }),
- vertex: {
- module,
- entryPoint: "vs",
- },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "src-alpha",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ module,
+ layouts: [pinUniformLayout, pinTextureLayout],
+ format,
+ blend: STRAIGHT_ALPHA_BLEND,
});
res = { pinPipeline, pinUniformLayout, pinTextureLayout };
@@ -137,16 +81,8 @@ function getCornerPinResources(device: GPUDevice, format: GPUTextureFormat) {
}
export const CornerPinWebGPURenderer: WebGPUNodeRenderer = async (args) => {
- const {
- ctx,
- props,
- drawChild,
- targetTexture,
- targetView,
- targetWidth,
- targetHeight,
- encoder,
- } = args;
+ const { ctx, props, targetTexture, targetWidth, targetHeight, encoder } =
+ args;
const { virtualMedia } = props;
const op = virtualMedia?.operation as any;
if (!op || op.op !== "CornerPin") return;
@@ -169,50 +105,20 @@ export const CornerPinWebGPURenderer: WebGPUNodeRenderer = async (args) => {
args.pass.end();
// 2. Render child into a temporary offscreen texture at its native size
- const childTex = ctx.renderer.getTemporaryTexture(sourceWidth, sourceHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const childView = childTex.createView();
-
- const clearPass = ctx.renderer.beginFrame(
- encoder,
- childView,
- { r: 0, g: 0, b: 0, a: 0 },
- sourceWidth,
- sourceHeight,
- "clear",
- );
- clearPass.end();
-
- ctx.renderer.pushScissor({
- x: 0,
- y: 0,
+ const { texture: childTex } = await drawChildToTexture(args, childMedia, {
width: sourceWidth,
height: sourceHeight,
- });
- ctx.renderer.pushIdentity();
-
- await drawChild(
- childMedia,
- {
+ props: {
...props,
containerWidth: sourceWidth,
containerHeight: sourceHeight,
virtualMedia: childMedia,
},
- childView,
- childTex,
- sourceWidth,
- sourceHeight,
- );
-
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// 3. Solve homography matrix mapping target corners to source corners
const points = op.points || [];
- const h = solveHomography(points);
+ const h = solveCornerPinHomography(points);
// 4. Setup uniform buffer with std140 layout alignment
const uniformData = new Float32Array(12);
@@ -259,39 +165,11 @@ export const CornerPinWebGPURenderer: WebGPUNodeRenderer = async (args) => {
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline!);
- renderPass.setBindGroup(0, uniformBindGroup);
- renderPass.setBindGroup(1, textureBindGroup);
- renderPass.draw(4);
- renderPass.end();
+ drawFullscreenPass(encoder, outTex.createView(), pipeline!, [
+ uniformBindGroup,
+ textureBindGroup,
+ ]);
// 6. Draw warped texture on top of targetView (with loadOp: "load" to keep background/prev layers)
- const outPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- outPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
-
- args.pass = outPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-corner-pin/src/shared/utils.ts b/nodes/node-corner-pin/src/shared/utils.ts
index c3149fc9..0b79f4b4 100644
--- a/nodes/node-corner-pin/src/shared/utils.ts
+++ b/nodes/node-corner-pin/src/shared/utils.ts
@@ -1,3 +1,5 @@
+import { solveHomography } from "@framefields/node-sdk";
+
export function getDefaultPoints(): { x: number; y: number }[] {
return [
{ x: 0, y: 0 }, // Top-Left (TL)
@@ -8,102 +10,15 @@ export function getDefaultPoints(): { x: number; y: number }[] {
}
/**
- * Computes a 3x3 homography matrix mapping normalized destination/target points (quad)
- * to normalized source points (unit square [0,1]^2).
- *
- * Target corners are input as percent coordinates (0-100) and normalized internally to [0,1].
- *
- * The homography H maps target (x, y) to source (u, v):
- * u = (h00*x + h01*y + h02) / (h20*x + h21*y + h22)
- * v = (h10*x + h11*y + h12) / (h20*x + h21*y + h22)
- *
- * We set h22 = 1.0.
+ * Homography mapping the corner-pin quad (percent coordinates, TL, TR, BL, BR)
+ * to the unit square it samples from. Falls back to the identity quad.
*/
-export function solveHomography(
+export function solveCornerPinHomography(
targetPoints: { x: number; y: number }[],
): number[] {
const pts =
targetPoints && targetPoints.length === 4
? targetPoints
: getDefaultPoints();
-
- // Source points: unit square corners matching TL, TR, BL, BR
- const src = [
- { x: 0.0, y: 0.0 }, // TL
- { x: 1.0, y: 0.0 }, // TR
- { x: 0.0, y: 1.0 }, // BL
- { x: 1.0, y: 1.0 }, // BR
- ];
-
- // Destination points normalized to [0,1]
- const dst = pts.map((p) => ({
- x: p.x / 100.0,
- y: p.y / 100.0,
- }));
-
- // Solve A * h = B where A is 8x8, B is 8x1
- const A: number[][] = [];
- const B: number[] = [];
-
- for (let i = 0; i < 4; i++) {
- const xi = dst[i].x;
- const yi = dst[i].y;
- const ui = src[i].x;
- const vi = src[i].y;
-
- A.push([xi, yi, 1.0, 0.0, 0.0, 0.0, -xi * ui, -yi * ui]);
- B.push(ui);
-
- A.push([0.0, 0.0, 0.0, xi, yi, 1.0, -xi * vi, -yi * vi]);
- B.push(vi);
- }
-
- const n = 8;
- for (let i = 0; i < n; i++) {
- // Find pivot row
- let maxRow = i;
- let maxVal = Math.abs(A[i][i]);
- for (let k = i + 1; k < n; k++) {
- if (Math.abs(A[k][i]) > maxVal) {
- maxVal = Math.abs(A[k][i]);
- maxRow = k;
- }
- }
-
- // Swap rows in A and B
- const tempRow = A[i];
- A[i] = A[maxRow];
- A[maxRow] = tempRow;
-
- const tempB = B[i];
- B[i] = B[maxRow];
- B[maxRow] = tempB;
-
- const pivot = A[i][i];
- if (Math.abs(pivot) < 1e-12) {
- // Singular or near-singular matrix. Continue with small fallback pivot.
- A[i][i] = pivot < 0 ? -1e-12 : 1e-12;
- }
-
- for (let k = i + 1; k < n; k++) {
- const factor = A[k][i] / A[i][i];
- for (let j = i; j < n; j++) {
- A[k][j] -= factor * A[i][j];
- }
- B[k] -= factor * B[i];
- }
- }
-
- // Back-substitution
- const h = new Array(8).fill(0.0);
- for (let i = n - 1; i >= 0; i--) {
- let sum = B[i];
- for (let j = i + 1; j < n; j++) {
- sum -= A[i][j] * h[j];
- }
- h[i] = sum / A[i][i];
- }
-
- // Returns h00, h01, h02, h10, h11, h12, h20, h21, h22=1.0
- return [h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], 1.0];
+ return solveHomography(pts.map((p) => ({ x: p.x / 100.0, y: p.y / 100.0 })));
}
diff --git a/nodes/node-crop/src/renderers/webgpu-renderer.ts b/nodes/node-crop/src/renderers/webgpu-renderer.ts
index af970271..e71dca11 100644
--- a/nodes/node-crop/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-crop/src/renderers/webgpu-renderer.ts
@@ -1,4 +1,8 @@
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ drawChildToTexture,
+ FULLSCREEN_QUAD_WGSL,
+} from "@framefields/node-sdk/gpu";
const maskWgsl = `
struct MaskUniforms {
@@ -11,27 +15,7 @@ struct MaskUniforms {
};
@group(0) @binding(0) var<uniform> u : MaskUniforms;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn sdRoundedRect(p: vec2<f32>, b: vec2<f32>, r: f32) -> f32 {
let q = abs(p) - b + r;
@@ -92,7 +76,6 @@ export const CropWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const {
ctx,
props,
- drawChild,
targetTexture,
targetView,
targetWidth,
@@ -163,45 +146,15 @@ export const CropWebGPURenderer: WebGPUNodeRenderer = async (args) => {
args.pass.end();
// 1. Render child into a temporary offscreen texture at its native size
- const childTex = ctx.renderer.getTemporaryTexture(sourceWidth, sourceHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const childView = childTex.createView();
-
- const clearPass = ctx.renderer.beginFrame(
- encoder,
- childView,
- { r: 0, g: 0, b: 0, a: 0 },
- sourceWidth,
- sourceHeight,
- "clear",
- );
- clearPass.end();
-
- ctx.renderer.pushScissor({
- x: 0,
- y: 0,
+ const { texture: childTex } = await drawChildToTexture(args, childMedia, {
width: sourceWidth,
height: sourceHeight,
- });
- ctx.renderer.pushIdentity();
-
- await drawChild(
- childMedia,
- {
+ props: {
...props,
containerWidth: sourceWidth,
containerHeight: sourceHeight,
},
- childView,
- childTex,
- sourceWidth,
- sourceHeight,
- );
-
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// Determine if we need complex masking (roundness/path)
const needsComplexMask = roundness > 0 || isPath || isRest;
diff --git a/nodes/node-curves/src/renderers/webgpu-renderer.ts b/nodes/node-curves/src/renderers/webgpu-renderer.ts
index e0ce7191..63380c88 100644
--- a/nodes/node-curves/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-curves/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,13 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
import { solveMonotonicSpline } from "../shared/spline.js";
import { computeHistogramFromEncoder } from "./histogram-computer.js";
@@ -12,26 +20,7 @@ const LUT_BYTES_PER_ROW = LUT_SIZE * 4; // 4 bytes per rgba8unorm texel
// ─── RGB Curves Shader ────────────────────────────────────────────────────────
const CURVES_RGB_SHADER = /* wgsl */ `
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@group(0) @binding(0) var t_lut : texture_2d<f32>;
@group(0) @binding(1) var s_lut : sampler;
@@ -67,12 +56,6 @@ struct Uniforms {
pad1: u32,
pad2: u32,
};
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
@group(0) @binding(0) var<uniform> u : Uniforms;
@group(0) @binding(1) var t_lut : texture_2d<f32>;
@group(0) @binding(2) var s_lut : sampler;
@@ -80,17 +63,7 @@ struct VSOut {
@group(1) @binding(0) var t_src : texture_2d<f32>;
@group(1) @binding(1) var s_src : sampler;
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0), vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0), vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn rgb_to_hsl(rgb: vec3<f32>) -> vec3<f32> {
let r = rgb.r; let g = rgb.g; let b = rgb.b;
@@ -190,46 +163,17 @@ function getRgbResources(
): RgbCurvesResources {
let res = rgbResourceCache.get(device);
if (!res) {
- const lutBGL = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
- const srcBGL = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const lutBGL = fragmentBindGroupLayout(device, ["texture", "sampler"]);
+ const srcBGL = fragmentBindGroupLayout(device, ["texture", "sampler"]);
const module = device.createShaderModule({
label: "curves-rgb.wgsl",
code: CURVES_RGB_SHADER,
});
- const pipeline = device.createRenderPipeline({
+ const pipeline = createFullscreenPipeline(device, {
label: "CurvesRgbPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [lutBGL, srcBGL],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: { module, entryPoint: "fs", targets: [{ format }] },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [lutBGL, srcBGL],
+ format,
});
res = { pipeline, lutBindGroupLayout: lutBGL, srcBindGroupLayout: srcBGL };
rgbResourceCache.set(device, res);
@@ -243,51 +187,21 @@ function getHslResources(
): HslCurvesResources {
let res = hslResourceCache.get(device);
if (!res) {
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
- const srcBGL = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, [
+ "uniform",
+ "texture",
+ "sampler",
+ ]);
+ const srcBGL = fragmentBindGroupLayout(device, ["texture", "sampler"]);
const module = device.createShaderModule({
label: "curves-hsl.wgsl",
code: CURVES_HSL_SHADER,
});
- const pipeline = device.createRenderPipeline({
+ const pipeline = createFullscreenPipeline(device, {
label: "CurvesHslPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [uniformLayout, srcBGL],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: { module, entryPoint: "fs", targets: [{ format }] },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [uniformLayout, srcBGL],
+ format,
});
res = { pipeline, uniformLayout, srcBindGroupLayout: srcBGL };
hslResourceCache.set(device, res);
@@ -322,7 +236,10 @@ function getLut(
const texture = build();
cache.set(configKey, texture);
if (cache.size > LUT_CACHE_LIMIT) {
- const [oldestKey, oldest] = cache.entries().next().value as [string, GPUTexture];
+ const [oldestKey, oldest] = cache.entries().next().value as [
+ string,
+ GPUTexture,
+ ];
cache.delete(oldestKey);
oldest.destroy();
}
@@ -434,12 +351,10 @@ export const CurvesWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as Record<string, unknown> & {
@@ -456,33 +371,7 @@ export const CurvesWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const curveType = (op.curveType as string) || "rgb";
// ── Capture child input frame ───────────────────────────────────────────
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// ── LUT / grading pass ──────────────────────────────────────────────────
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
@@ -500,22 +389,12 @@ export const CurvesWebGPURenderer: WebGPUNodeRenderer = async (args) => {
green: op.green,
blue: op.blue,
});
- const lut = getLut(ctx.device, configKey, () => buildRgbLut(ctx.device, op));
+ const lut = getLut(ctx.device, configKey, () =>
+ buildRgbLut(ctx.device, op),
+ );
const { pipeline, lutBindGroupLayout, srcBindGroupLayout } =
getRgbResources(ctx.device, ctx.renderer.format);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: lutBindGroupLayout,
entries: [
@@ -523,18 +402,13 @@ export const CurvesWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 1, resource: sampler },
],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
srcBindGroupLayout,
tmpTex,
sampler,
),
- );
- renderPass.draw(4);
- renderPass.end();
+ ]);
} else {
const hslCurveKey = HSL_CURVE_KEY_MAP[curveType];
const hslPoints = (op[hslCurveKey] as { x: number; y: number }[]) || [
@@ -542,7 +416,9 @@ export const CurvesWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ x: 1, y: 0.5 },
];
const configKey = JSON.stringify({ curveType, points: hslPoints });
- const lut = getLut(ctx.device, configKey, () => buildHslLut(ctx.device, hslPoints));
+ const lut = getLut(ctx.device, configKey, () =>
+ buildHslLut(ctx.device, hslPoints),
+ );
const uniformData = new Uint32Array([
HSL_MODE_MAP[curveType] ?? 0,
0,
@@ -554,19 +430,7 @@ export const CurvesWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx.device,
ctx.renderer.format,
);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [
@@ -575,37 +439,18 @@ export const CurvesWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 2, resource: sampler },
],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
srcBindGroupLayout,
tmpTex,
sampler,
),
- );
- renderPass.draw(4);
- renderPass.end();
+ ]);
}
// ── Histogram compute — runs in the SAME encoder so outTex data is valid ─
computeHistogramFromEncoder(ctx.device, encoder, outTex, nodeId);
// ── Final composite to target ──────────────────────────────────────────
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: props.opacity ?? 1 },
- );
- (args as Record<string, unknown>).pass = finalPass;
+ drawTextureToTarget(args, outTex, props.opacity ?? 1);
};
diff --git a/nodes/node-deflicker/src/index.ts b/nodes/node-deflicker/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-deflicker/src/index.ts
+++ b/nodes/node-deflicker/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-deflicker/src/renderers/webgpu-renderer.ts b/nodes/node-deflicker/src/renderers/webgpu-renderer.ts
index bb63c18d..afa52d2f 100644
--- a/nodes/node-deflicker/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-deflicker/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,13 @@
/// <reference types="webgpu" />
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+
+import {
+ resolveFrameNumber,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ drawChildToTexture,
+ drawTextureToTarget,
+} from "@framefields/node-sdk/gpu";
import { TemporalDeflickerPipeline } from "@framefields/tensor-webgpu";
interface TemporalDeflickerOp {
@@ -26,52 +34,6 @@ interface TemporalDeflickerOp {
>;
}
-function resolveVal(
- val: unknown,
- fallback: number,
- frame: number,
- fps: number,
-): number {
- if (val === undefined || val === null) return fallback;
- if (typeof val === "number") return Number.isFinite(val) ? val : fallback;
- if (typeof val === "object" && val !== null) {
- if (
- "get" in (val as Record<string, unknown>) &&
- typeof (val as { get: unknown }).get === "function"
- ) {
- try {
- const res = (val as { get: (ctx?: unknown) => unknown }).get({
- frame,
- fps,
- time: fps > 0 ? frame / fps : 0,
- duration: 0,
- durationMs: 0,
- progress: 0,
- deltaTime: fps > 0 ? 1 / fps : 0,
- });
- const n = Number(res);
- if (Number.isFinite(n)) return n;
- } catch {
- // fallback
- }
- }
- if ("value" in (val as Record<string, unknown>)) {
- const n = Number((val as { value: unknown }).value);
- if (Number.isFinite(n)) return n;
- }
- }
- if (typeof val === "function") {
- try {
- const n = Number(val({ frame, fps, time: fps > 0 ? frame / fps : 0 }));
- if (Number.isFinite(n)) return n;
- } catch {
- // fallback
- }
- }
- const n = Number(val);
- return Number.isFinite(n) ? n : fallback;
-}
-
function resolveProp(
op: TemporalDeflickerOp,
fieldName:
@@ -101,7 +63,7 @@ function resolveProp(
raw = op[fieldName];
}
- const resolved = resolveVal(raw, defaultValue, frame, fps);
+ const resolved = resolveFrameNumber(raw, defaultValue, frame, fps);
return Math.max(minVal, Math.min(maxVal, resolved));
}
@@ -122,17 +84,7 @@ function getOrCreatePipeline(
export const TemporalDeflickerWebGPURenderer: WebGPUNodeRenderer = async (
args,
) => {
- const {
- ctx,
- encoder,
- pass,
- targetView,
- targetTexture,
- targetWidth,
- targetHeight,
- props,
- drawChild,
- } = args;
+ const { ctx, encoder, pass, targetWidth, targetHeight, props } = args;
const op = props.virtualMedia?.operation as TemporalDeflickerOp | undefined;
if (!op || (op.op !== "TemporalDeflicker" && op.op !== "Deflicker")) {
@@ -154,39 +106,14 @@ export const TemporalDeflickerWebGPURenderer: WebGPUNodeRenderer = async (
const fps = props.fps || 30;
// 1. Draw child source media into a clean temporary texture
- const srcTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const srcView = srcTex.createView();
-
- const srcClearPass = ctx.renderer.beginFrame(
- encoder,
- srcView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- srcClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- sourceMedia,
- {
+ const { texture: srcTex } = await drawChildToTexture(args, sourceMedia, {
+ props: {
...props,
virtualMedia: sourceMedia,
containerWidth: width,
containerHeight: height,
},
- srcView,
- srcTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// End active pass from child drawing
args.pass.end();
@@ -247,22 +174,6 @@ export const TemporalDeflickerWebGPURenderer: WebGPUNodeRenderer = async (
);
// 4. Render stabilized output to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "load",
- );
-
const opacity = typeof op.opacity === "number" ? op.opacity : 1.0;
- ctx.renderer.drawTexture(
- finalPass,
- deflickeredTex,
- { x: 0, y: 0, width, height },
- { opacity },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, deflickeredTex, opacity);
};
diff --git a/nodes/node-displacement-map/src/renderers/webgpu-renderer.ts b/nodes/node-displacement-map/src/renderers/webgpu-renderer.ts
index b6089c11..359f51c3 100644
--- a/nodes/node-displacement-map/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-displacement-map/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,17 @@
/// <reference types="webgpu" />
import type { SignalData, VirtualMediaData } from "@framefields/core";
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
// FIX (P1): `mapMedia` is now a first-class, typed field instead of being
@@ -65,27 +76,7 @@ struct DisplacementUniforms {
@group(3) @binding(0) var sigXTex : texture_2d<f32>;
@group(3) @binding(1) var sigYTex : texture_2d<f32>;
@group(3) @binding(2) var sigSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn extractChannel(color: vec4<f32>, channel: f32) -> f32 {
if (channel < 0.5) { return color.r; }
@@ -149,18 +140,6 @@ fn applyWrap(coord: f32, mode: f32) -> f32 {
}
`;
-interface DeviceDisplacementResources {
- uniformLayout: GPUBindGroupLayout;
- srcTextureLayout: GPUBindGroupLayout;
- mapTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<
- GPUDevice,
- DeviceDisplacementResources
->();
// NOTE (P3, not changed): module-scoped and reused across every invocation
// (including recursive/nested displacement nodes). Currently safe because
// there is no `await` between filling this array and the synchronous
@@ -169,80 +148,13 @@ const deviceResourceCache = new WeakMap<
// this function-scoped if that invariant ever changes.
const uniformData = new Float32Array(8);
-function getDeviceLayouts(device: GPUDevice): DeviceDisplacementResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const srcTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const mapTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- srcTextureLayout,
- mapTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ srcTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ mapTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: texturesLayout(device, 2),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getDisplacementResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -255,23 +167,16 @@ function getDisplacementResources(device: GPUDevice, format: GPUTextureFormat) {
code: DISPLACEMENT_MAP_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `DisplacementMapPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.srcTextureLayout,
- layouts.mapTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.srcTextureLayout,
+ layouts.mapTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -366,12 +271,10 @@ export const DisplacementMapWebGPURenderer: WebGPUNodeRenderer = async (
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as DisplacementMapOp | undefined;
@@ -395,39 +298,13 @@ export const DisplacementMapWebGPURenderer: WebGPUNodeRenderer = async (
const strengthYInfo = resolveSignalField(op, "strengthY", 50);
// --- Draw source child into srcTex ---
- const srcTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const srcView = srcTex.createView();
-
- const srcClearPass = ctx.renderer.beginFrame(
- encoder,
- srcView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- srcClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- sourceMedia,
- {
+ const { texture: srcTex } = await drawChildToTexture(args, sourceMedia, {
+ props: {
...props,
virtualMedia: sourceMedia,
renderId: `${props.renderId}-c0`,
- excludeTextures: [...(props.excludeTextures || []), srcTex],
},
- srcView,
- srcTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// FIX (P0 - CRITICAL): drawChild leaves its own last-active pass open on
// `args.pass` (targeting srcView), matching the same "caller closes it"
@@ -441,40 +318,15 @@ export const DisplacementMapWebGPURenderer: WebGPUNodeRenderer = async (
args.pass.end();
// --- Draw map child into mapTex ---
- const mapTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- srcTex,
- ]);
- const mapView = mapTex.createView();
-
- const mapClearPass = ctx.renderer.beginFrame(
- encoder,
- mapView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- mapClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- mapMedia,
- {
+ const { texture: mapTex } = await drawChildToTexture(args, mapMedia, {
+ props: {
...props,
virtualMedia: mapMedia,
renderId: `${props.renderId}-c1`,
- excludeTextures: [...(props.excludeTextures || []), srcTex, mapTex],
+ excludeTextures: [...(props.excludeTextures || []), srcTex],
},
- mapView,
- mapTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ exclude: [srcTex],
+ });
// End active pass from child drawing
args.pass.end();
@@ -494,13 +346,7 @@ export const DisplacementMapWebGPURenderer: WebGPUNodeRenderer = async (
let sigXView: GPUTextureView;
let sigYView: GPUTextureView;
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
if (strengthXInfo.hasSignal && strengthXInfo.sd) {
sigXView = signalRegistry.getOrCreate2DTextureView(
@@ -559,45 +405,23 @@ export const DisplacementMapWebGPURenderer: WebGPUNodeRenderer = async (
]);
// --- Render displacement pass ---
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
srcLayout,
srcTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 2,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
mapLayout,
mapTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 3,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -606,26 +430,8 @@ export const DisplacementMapWebGPURenderer: WebGPUNodeRenderer = async (
{ binding: 2, resource: sampler },
],
}),
- );
-
- renderPass.draw(4);
- renderPass.end();
+ ]);
// --- Final pass: draw result to targetView ---
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-extract-frame/src/renderers/webgpu-renderer.ts b/nodes/node-extract-frame/src/renderers/webgpu-renderer.ts
index e6f179e0..36fcfeed 100644
--- a/nodes/node-extract-frame/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-extract-frame/src/renderers/webgpu-renderer.ts
@@ -1,4 +1,5 @@
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import { drawChildToTexture } from "@framefields/node-sdk/gpu";
/**
* WebGPU renderer for the ExtractFrame operation.
@@ -8,16 +9,7 @@ import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
* and the child is rendered at the corresponding timestamp.
*/
export const ExtractFrameWebGPURenderer: WebGPUNodeRenderer = async (args) => {
- const {
- ctx,
- props,
- drawChild,
- targetView,
- targetTexture,
- targetWidth,
- targetHeight,
- encoder,
- } = args;
+ const { ctx, props, targetView, targetWidth, targetHeight, encoder } = args;
const { virtualMedia, opacity = 1 } = props;
const op = virtualMedia?.operation as Record<string, unknown> | undefined;
@@ -43,34 +35,10 @@ export const ExtractFrameWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const containerHeight = props.containerHeight ?? ctx.surface.height;
// Render the child into a temporary texture at the specified frame
- const childTex = ctx.renderer.getTemporaryTexture(sourceWidth, sourceHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const childView = childTex.createView();
-
- const clearPass = ctx.renderer.beginFrame(
- encoder,
- childView,
- { r: 0, g: 0, b: 0, a: 0 },
- sourceWidth,
- sourceHeight,
- "clear",
- );
- clearPass.end();
-
- ctx.renderer.pushScissor({
- x: 0,
- y: 0,
+ const { texture: childTex } = await drawChildToTexture(args, childMedia, {
width: sourceWidth,
height: sourceHeight,
- });
- ctx.renderer.pushIdentity();
-
- // Override the child's render props to set the specific frame/timestamp
- await drawChild(
- childMedia,
- {
+ props: {
...props,
containerWidth: sourceWidth,
containerHeight: sourceHeight,
@@ -79,14 +47,7 @@ export const ExtractFrameWebGPURenderer: WebGPUNodeRenderer = async (args) => {
timestampSec,
forceWait: true,
},
- childView,
- childTex,
- sourceWidth,
- sourceHeight,
- );
-
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// Draw the result to the target
const outPass = ctx.renderer.beginFrame(
diff --git a/nodes/node-film-grain/src/renderers/webgpu-renderer.ts b/nodes/node-film-grain/src/renderers/webgpu-renderer.ts
index c945250c..a58454d6 100644
--- a/nodes/node-film-grain/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-film-grain/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,16 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTiming,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
interface FilmGrainOp {
@@ -52,27 +62,7 @@ struct FilmUniforms {
@group(1) @binding(1) var samp : sampler;
@group(2) @binding(0) var signalTex : texture_2d<f32>;
@group(2) @binding(1) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn hash2D(p: vec2<f32>) -> f32 {
var p3 = fract(vec3<f32>(p.xyx) * 0.1031);
@@ -171,69 +161,14 @@ fn frameJitter(frameIndex: f32) -> vec2<f32> {
}
`;
-interface DeviceFilmGrainResources {
- filmUniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceFilmGrainResources>();
const filmGrainUniformData = new Float32Array(12);
-function getDeviceLayouts(device: GPUDevice): DeviceFilmGrainResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const filmUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- filmUniformLayout,
- singleTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ filmUniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getFilmGrainResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -246,22 +181,15 @@ function getFilmGrainResources(device: GPUDevice, format: GPUTextureFormat) {
code: FILM_GRAIN_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `FilmGrainPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.filmUniformLayout,
- layouts.singleTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module: filmModule, entryPoint: "vs" },
- fragment: {
- module: filmModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: filmModule,
+ layouts: [
+ layouts.filmUniformLayout,
+ layouts.singleTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -279,12 +207,10 @@ export const FilmGrainWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -360,34 +286,7 @@ export const FilmGrainWebGPURenderer: WebGPUNodeRenderer = async (args) => {
}
// 1. Draw input child to temporary texture
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// End any active pass from drawing child
args.pass.end();
@@ -404,13 +303,7 @@ export const FilmGrainWebGPURenderer: WebGPUNodeRenderer = async (args) => {
// Fetch dynamic signal texture view on-demand
let signalView: GPUTextureView;
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
if (hasStaticSignal && sd) {
signalView = signalRegistry.getOrCreate2DTextureView(
@@ -462,36 +355,17 @@ export const FilmGrainWebGPURenderer: WebGPUNodeRenderer = async (args) => {
]);
// 5. Run single-pass Film Grain shader
- const filmPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- filmPass.setPipeline(pipeline);
- filmPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- filmPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout,
tmpTex,
sampler,
),
- );
- filmPass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -499,26 +373,8 @@ export const FilmGrainWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 1, resource: sampler },
],
}),
- );
- filmPass.draw(4);
- filmPass.end();
+ ]);
// 6. Draw results to final targetView using opacity blend
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: resolveVal(op.opacity, 1.0) },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, resolveVal(op.opacity, 1.0));
};
diff --git a/nodes/node-flip/src/renderers/webgpu-renderer.ts b/nodes/node-flip/src/renderers/webgpu-renderer.ts
index 8b0b7c4d..f1e842ed 100644
--- a/nodes/node-flip/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-flip/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,14 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ STRAIGHT_ALPHA_BLEND,
+} from "@framefields/node-sdk/gpu";
interface FlipOp {
op: "Flip";
@@ -34,27 +43,7 @@ struct FlipUniforms {
@group(0) @binding(0) var<uniform> u : FlipUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
var uv = in.uv;
@@ -113,30 +102,9 @@ function getFlipResources(
const cached = deviceResourceCache.get(device);
if (cached) return cached;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, ["texture", "sampler"]);
const shaderModule = device.createShaderModule({
code: WGSL_FLIP_SHADER,
@@ -146,36 +114,11 @@ function getFlipResources(
bindGroupLayouts: [uniformLayout, textureLayout],
});
- const pipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: {
- module: shaderModule,
- entryPoint: "vs",
- },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- targets: [
- {
- format: targetFormat,
- blend: {
- color: {
- srcFactor: "src-alpha",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ const pipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: pipelineLayout,
+ format: targetFormat,
+ blend: STRAIGHT_ALPHA_BLEND,
});
const res: DeviceFlipResources = {
@@ -192,12 +135,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as FlipOp | undefined;
@@ -217,44 +158,16 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
childMedia.metadata?.height ?? props.containerHeight ?? height;
// 1. Render child into offscreen texture
- const childTex = ctx.renderer.getTemporaryTexture(sourceWidth, sourceHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const childView = childTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- childView,
- { r: 0, g: 0, b: 0, a: 0 },
- sourceWidth,
- sourceHeight,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({
- x: 0,
- y: 0,
+ const { texture: childTex } = await drawChildToTexture(args, childMedia, {
width: sourceWidth,
height: sourceHeight,
- });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
+ props: {
...props,
containerWidth: sourceWidth,
containerHeight: sourceHeight,
virtualMedia: childMedia,
},
- childView,
- childTex,
- sourceWidth,
- sourceHeight,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// 2. Fetch resources & configure flip parameters
const {
@@ -322,38 +235,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(0, uBindGroup);
- renderPass.setBindGroup(1, tBindGroup);
- renderPass.draw(4);
- renderPass.end();
-
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
+ uBindGroup,
+ tBindGroup,
+ ]);
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1.0);
};
diff --git a/nodes/node-gradient-map/src/renderers/webgpu-renderer.ts b/nodes/node-gradient-map/src/renderers/webgpu-renderer.ts
index ff5cd5ed..ae0550ff 100644
--- a/nodes/node-gradient-map/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-gradient-map/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,15 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTiming,
+} from "@framefields/node-sdk/gpu";
import { parseColor, signalRegistry } from "@framefields/webgpu-renderers";
import type { GradientMapOperation, GradientStop } from "../shared/config.js";
@@ -56,27 +65,7 @@ struct GradientMapUniforms {
@group(1) @binding(1) var samp : sampler;
@group(2) @binding(0) var opacitySigTex : texture_2d<f32>;
@group(2) @binding(1) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn ditherNoise(pos: vec2<f32>) -> f32 {
let p = fract(pos * vec2<f32>(0.1031, 0.1030));
@@ -149,62 +138,34 @@ function getGradientMapResources(
let res = deviceResourceCache.get(device);
if (res) return res;
- const uniformLayout = device.createBindGroupLayout({
- label: "GradientMapUniformLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(
+ device,
+ ["uniform"],
+ "GradientMapUniformLayout",
+ );
- const textureLayout = device.createBindGroupLayout({
- label: "GradientMapTextureLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(
+ device,
+ ["texture", "sampler"],
+ "GradientMapTextureLayout",
+ );
- const signalLayout = device.createBindGroupLayout({
- label: "GradientMapSignalLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalLayout = fragmentBindGroupLayout(
+ device,
+ ["texture", "sampler"],
+ "GradientMapSignalLayout",
+ );
const module = device.createShaderModule({
label: "GradientMapShaderModule",
code: WGSL_GRADIENT_MAP_SHADER,
});
- const pipeline = device.createRenderPipeline({
+ const pipeline = createFullscreenPipeline(device, {
label: "GradientMapPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [uniformLayout, textureLayout, signalLayout],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: { module, entryPoint: "fs", targets: [{ format }] },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [uniformLayout, textureLayout, signalLayout],
+ format,
});
res = {
@@ -224,12 +185,10 @@ export const GradientMapWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -247,37 +206,7 @@ export const GradientMapWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const width = targetWidth;
const height = targetHeight;
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
- ...props,
- virtualMedia: childMedia,
- },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// Parse stops
const rawStops: GradientStop[] = Array.isArray(op.stops)
@@ -313,13 +242,7 @@ export const GradientMapWebGPURenderer: WebGPUNodeRenderer = async (args) => {
opacityVal = Number(opacitySignalInput.outputItem.data ?? 1.0);
}
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
let opacityView: GPUTextureView;
if (hasOpacitySig && opacitySigData) {
@@ -379,36 +302,17 @@ export const GradientMapWebGPURenderer: WebGPUNodeRenderer = async (args) => {
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [{ binding: 0, resource: { buffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
textureLayout,
tmpTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: signalLayout,
entries: [
@@ -416,25 +320,7 @@ export const GradientMapWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 1, resource: sampler },
],
}),
- );
- renderPass.draw(4);
- renderPass.end();
-
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: props.opacity ?? 1 },
- );
+ ]);
- (args as unknown as { pass: GPURenderPassEncoder }).pass = finalPass;
+ drawTextureToTarget(args, outTex, props.opacity ?? 1);
};
diff --git a/nodes/node-halftone-screen/src/index.ts b/nodes/node-halftone-screen/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-halftone-screen/src/index.ts
+++ b/nodes/node-halftone-screen/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-halftone-screen/src/renderers/webgpu-renderer.ts b/nodes/node-halftone-screen/src/renderers/webgpu-renderer.ts
index deb4f091..550513db 100644
--- a/nodes/node-halftone-screen/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-halftone-screen/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,22 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { parseColor, signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ unwrapFrameParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
+import { parseColor } from "@framefields/webgpu-renderers";
import type { HalftoneScreenOperation } from "../shared/config.js";
interface HalftoneScreenOp extends HalftoneScreenOperation {
@@ -71,27 +86,7 @@ struct HalftoneUniforms {
@group(2) @binding(6) var blkSigTex : texture_2d<f32>;
@group(2) @binding(7) var opacitySigTex : texture_2d<f32>;
@group(2) @binding(8) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
const PI: f32 = 3.141592653589793;
@@ -303,103 +298,12 @@ fn evalDotCoverage(
}
`;
-interface DeviceHalftoneResources {
- uniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceHalftoneResources>();
-
-function getDeviceLayouts(device: GPUDevice): DeviceHalftoneResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 7,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 8,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- singleTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: texturesLayout(device, 8),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getHalftoneResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -412,22 +316,15 @@ function getHalftoneResources(device: GPUDevice, format: GPUTextureFormat) {
code: WGSL_HALFTONE_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `HalftonePipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.singleTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module: halftoneModule, entryPoint: "vs" },
- fragment: {
- module: halftoneModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: halftoneModule,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.singleTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -447,12 +344,10 @@ export const HalftoneScreenWebGPURenderer: WebGPUNodeRenderer = async (
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -511,28 +406,11 @@ export const HalftoneScreenWebGPURenderer: WebGPUNodeRenderer = async (
Math.min(maxVal, Number(input.outputItem.data ?? defaultValue)),
);
} else {
- let rawVal = op[configKey] as unknown;
- if (rawVal !== undefined && rawVal !== null) {
- if (typeof rawVal === "object") {
- if (
- "get" in (rawVal as Record<string, unknown>) &&
- typeof (rawVal as Record<string, unknown>).get === "function"
- ) {
- rawVal = (
- rawVal as {
- get: (ctx: { frame: number; fps: number }) => unknown;
- }
- ).get({ frame: compositionFrame, fps });
- } else if ("value" in (rawVal as Record<string, unknown>)) {
- rawVal = (rawVal as { value: unknown }).value;
- }
- } else if (typeof rawVal === "function") {
- rawVal = (rawVal as (f: number, fps: number) => unknown)(
- compositionFrame,
- fps,
- );
- }
- }
+ const rawVal = unwrapFrameParam(
+ op[configKey],
+ compositionFrame,
+ fps,
+ ).value;
val = Math.max(
minVal,
Math.min(maxVal, Number(rawVal ?? defaultValue)),
@@ -556,34 +434,7 @@ export const HalftoneScreenWebGPURenderer: WebGPUNodeRenderer = async (
const opacityRes = resolveBindable("opacity", 1.0, 0.0, 1.0);
// 2. Draw child media
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// 3. Fetch layout and resources
const {
@@ -595,35 +446,21 @@ export const HalftoneScreenWebGPURenderer: WebGPUNodeRenderer = async (
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: { val: number; hasStaticSig: boolean; sd: HalftoneSignalData | null },
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const freqView = getSignalView(freqRes, "freq_sig");
const angleView = getSignalView(angleRes, "angle_sig");
@@ -690,36 +527,17 @@ export const HalftoneScreenWebGPURenderer: WebGPUNodeRenderer = async (
]);
// 6. Run Halftone Screen Render Pass
- const halftonePass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- halftonePass.setPipeline(pipeline);
- halftonePass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- halftonePass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout,
tmpTex,
sampler,
),
- );
- halftonePass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -734,26 +552,8 @@ export const HalftoneScreenWebGPURenderer: WebGPUNodeRenderer = async (
{ binding: 8, resource: sampler },
],
}),
- );
- halftonePass.draw(4);
- halftonePass.end();
+ ]);
// 7. Output result back to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex);
};
diff --git a/nodes/node-halftone-screen/src/shared/config.ts b/nodes/node-halftone-screen/src/shared/config.ts
index e62db1fa..af961308 100644
--- a/nodes/node-halftone-screen/src/shared/config.ts
+++ b/nodes/node-halftone-screen/src/shared/config.ts
@@ -2,6 +2,7 @@ import {
ColorSchema,
configBuilder,
ImageResultSchema,
+ OperationInputsSchema,
VideoResultSchema,
} from "@framefields/node-sdk";
import { z } from "zod";
@@ -124,21 +125,7 @@ export const HalftoneScreenOperationSchema =
yellowAngleHandleId: z.string().nullable().optional(),
blackAngleHandleId: z.string().nullable().optional(),
opacityHandleId: z.string().nullable().optional(),
- inputs: z
- .record(
- z.string(),
- z.object({
- connectionValid: z.boolean(),
- outputItem: z
- .object({
- type: z.string(),
- data: z.unknown(),
- })
- .nullable()
- .optional(),
- }),
- )
- .optional(),
+ inputs: OperationInputsSchema.optional(),
});
export type HalftoneScreenOperation = z.infer<
diff --git a/nodes/node-high-pass/src/index.ts b/nodes/node-high-pass/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-high-pass/src/index.ts
+++ b/nodes/node-high-pass/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-high-pass/src/renderers/webgpu-renderer.ts b/nodes/node-high-pass/src/renderers/webgpu-renderer.ts
index b4035a09..4f9e233d 100644
--- a/nodes/node-high-pass/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-high-pass/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,20 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ resolveHandleParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { MAX_RADIUS } from "../shared/config.js";
interface HighPassOp {
@@ -47,27 +60,7 @@ struct HighPassUniforms {
@group(2) @binding(0) var radiusSigTex : texture_2d<f32>;
@group(2) @binding(1) var contrastSigTex : texture_2d<f32>;
@group(2) @binding(2) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let dimensions = vec2<f32>(textureDimensions(tex));
@@ -152,55 +145,15 @@ function getHighPassResources(
const cached = deviceResourceCache.get(device);
if (cached) return cached;
- const highPassUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const highPassUniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- ],
- });
+ const singleTextureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ "texture",
+ ]);
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalTextureLayout = texturesLayout(device, 2);
const shaderModule = device.createShaderModule({
code: WGSL_HIGHPASS_UNIFORMS,
@@ -214,20 +167,10 @@ function getHighPassResources(
],
});
- const highPassPipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: {
- module: shaderModule,
- entryPoint: "vs",
- },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- targets: [{ format: targetFormat }],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ const highPassPipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: pipelineLayout,
+ format: targetFormat,
});
const res: DeviceHighPassResources = {
@@ -245,12 +188,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as HighPassOp | undefined;
@@ -273,34 +214,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
fallbackVal: number,
clampMin = 0,
clampMax = 1000,
- ): { val: number; hasStaticSig: boolean; sd: HighPassSignalData | null } => {
- if (handleId && op.inputs?.[handleId]?.connectionValid) {
- const outputItem = op.inputs[handleId].outputItem;
- if (outputItem?.type === "Number") {
- const numVal =
- typeof outputItem.data === "number"
- ? outputItem.data
- : ((outputItem.data as { value?: number })?.value ?? fallbackVal);
- return {
- val: Math.max(clampMin, Math.min(clampMax, numVal)),
- hasStaticSig: false,
- sd: null,
- };
- }
- if (outputItem?.type === "Signal" && outputItem.data) {
- return {
- val: fallbackVal,
- hasStaticSig: true,
- sd: outputItem.data as HighPassSignalData,
- };
- }
- }
- return {
- val: Math.max(clampMin, Math.min(clampMax, fallbackVal)),
- hasStaticSig: false,
- sd: null,
- };
- };
+ ) =>
+ resolveHandleParam<HighPassSignalData>(
+ op.inputs,
+ handleId,
+ fallbackVal,
+ clampMin,
+ clampMax,
+ );
const radiusRes = resolveField(
op.radiusHandleId,
@@ -316,34 +237,7 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
);
// 1. Render child media into temporary input texture
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// 2. Fetch static resources and compile pipeline once
const {
@@ -355,35 +249,21 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: { val: number; hasStaticSig: boolean; sd: HighPassSignalData | null },
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const radiusView = getSignalView(radiusRes, "radius_sig");
const contrastView = getSignalView(contrastRes, "contrast_sig");
@@ -436,28 +316,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
highPassUniformData[7] = 0.0; // isSecondPass = false
const hBuffer = ctx.renderer.getTemporaryBuffer(highPassUniformData);
- const hPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex1.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- hPass.setPipeline(pipeline);
- hPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex1.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: hBuffer } }],
}),
- );
- hPass.setBindGroup(1, createHighPassBindGroup1(tmpTex, tmpTex));
- hPass.setBindGroup(2, sigBindGroup);
- hPass.draw(4);
- hPass.end();
+ createHighPassBindGroup1(tmpTex, tmpTex),
+ sigBindGroup,
+ ]);
// Pass 2: Vertical Gaussian convolution + High Pass Subtraction against original tmpTex
highPassUniformData[0] = 0.0;
@@ -465,45 +331,15 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
highPassUniformData[7] = 1.0; // isSecondPass = true
const vBuffer = ctx.renderer.getTemporaryBuffer(highPassUniformData);
- const vPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex2.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- vPass.setPipeline(pipeline);
- vPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex2.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: vBuffer } }],
}),
- );
- vPass.setBindGroup(1, createHighPassBindGroup1(outTex1, tmpTex));
- vPass.setBindGroup(2, sigBindGroup);
- vPass.draw(4);
- vPass.end();
+ createHighPassBindGroup1(outTex1, tmpTex),
+ sigBindGroup,
+ ]);
// 3. Final Pass: Draw result back to targetView using standard drawTexture for blending/opacity
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex2,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex2, op.opacity ?? 1.0);
};
diff --git a/nodes/node-kenburns/src/renderers/webgpu-renderer.ts b/nodes/node-kenburns/src/renderers/webgpu-renderer.ts
index 0c0600b3..38ca7b48 100644
--- a/nodes/node-kenburns/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-kenburns/src/renderers/webgpu-renderer.ts
@@ -1,4 +1,11 @@
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
import type { KenBurnsKeyframe } from "../shared/config.js";
const easeIn = (t: number): number => t * t;
@@ -183,27 +190,7 @@ struct MotionBlurUniforms {
@group(0) @binding(0) var<uniform> u : MotionBlurUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let uv = in.uv;
@@ -242,41 +229,22 @@ const motionBlurData = new Float32Array(12);
function getMotionBlurResources(device: GPUDevice, format: GPUTextureFormat) {
let res = deviceResourceCache.get(device);
if (!res) {
- const motionBlurUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const motionBlurUniformLayout = fragmentBindGroupLayout(device, [
+ "uniform",
+ ]);
+ const singleTextureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
const module = device.createShaderModule({
label: "kenburns_motionblur.wgsl",
code: motionBlurWgsl,
});
- const motionBlurPipeline = device.createRenderPipeline({
+ const motionBlurPipeline = createFullscreenPipeline(device, {
label: "MotionBlurPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [motionBlurUniformLayout, singleTextureLayout],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: { module, entryPoint: "fs", targets: [{ format }] },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [motionBlurUniformLayout, singleTextureLayout],
+ format,
});
res = { motionBlurPipeline, motionBlurUniformLayout, singleTextureLayout };
deviceResourceCache.set(device, res);
@@ -294,7 +262,6 @@ export const KenBurnsWebGPURenderer: WebGPUNodeRenderer = async (args) => {
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const {
@@ -367,49 +334,18 @@ export const KenBurnsWebGPURenderer: WebGPUNodeRenderer = async (args) => {
// cropped region ourselves using drawTextureRegion — which does not depend
// on the transform stack at all.
//
- const tempTex = ctx.renderer.getTemporaryTexture(sourceWidth, sourceHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tempView = tempTex.createView();
-
// End the current pass before starting child rendering.
pass.end();
- // Clear the temporary texture to avoid garbage from previous frames
- const clearPass = ctx.renderer.beginFrame(
- encoder,
- tempView,
- { r: 0, g: 0, b: 0, a: 0 },
- sourceWidth,
- sourceHeight,
- "clear",
- );
- clearPass.end();
-
- ctx.renderer.pushScissor({
- x: 0,
- y: 0,
+ const { texture: tempTex } = await drawChildToTexture(args, childMedia, {
width: sourceWidth,
height: sourceHeight,
- });
- ctx.renderer.pushIdentity();
-
- await drawChild(
- childMedia,
- {
+ props: {
...props,
containerWidth: sourceWidth,
containerHeight: sourceHeight,
},
- tempView,
- tempTex,
- sourceWidth,
- sourceHeight,
- );
-
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
const motionBlurSize = op.motionBlurSize ?? 1.5;
let blurTex: GPUTexture | null = null;
@@ -444,36 +380,18 @@ export const KenBurnsWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const blurView = blurTex.createView();
// 2. Render blurred result into blurTex
- const blurPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: blurView,
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- blurPass.setPipeline(pipeline!);
- blurPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, blurView, pipeline!, [
ctx.device.createBindGroup({
layout: uLayout!,
entries: [{ binding: 0, resource: { buffer: uBuffer } }],
}),
- );
- blurPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout!,
tempTex,
sampler,
),
- );
- blurPass.draw(4);
- blurPass.end();
+ ]);
}
// ── 6. Composite the cropped source onto the output ──────────────────────
diff --git a/nodes/node-layer-style/src/index.ts b/nodes/node-layer-style/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-layer-style/src/index.ts
+++ b/nodes/node-layer-style/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-layer-style/src/renderers/webgpu-renderer.ts b/nodes/node-layer-style/src/renderers/webgpu-renderer.ts
index 4f8a434f..d8858aab 100644
--- a/nodes/node-layer-style/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-layer-style/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,13 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ PREMULTIPLIED_BLEND,
+} from "@framefields/node-sdk/gpu";
import {
computeLayerStylePadding,
type LayerStyleNodeConfig,
@@ -145,27 +153,7 @@ struct LayerStyleUniforms {
@group(0) @binding(0) var<uniform> u : LayerStyleUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn sampleAlpha(uv: vec2<f32>) -> f32 {
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
@@ -652,65 +640,19 @@ function getLayerStyleResources(
code: WGSL_LAYER_STYLE_SHADER,
});
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, ["texture", "sampler"]);
const pipelineLayout = device.createPipelineLayout({
bindGroupLayouts: [uniformLayout, textureLayout],
});
- const pipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: {
- module,
- entryPoint: "vs",
- },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ const pipeline = createFullscreenPipeline(device, {
+ module,
+ layouts: pipelineLayout,
+ format,
+ blend: PREMULTIPLIED_BLEND,
});
res = { pipeline, uniformLayout, textureLayout };
@@ -725,7 +667,6 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
@@ -1152,39 +1093,11 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(0, uBindGroup);
- renderPass.setBindGroup(1, tBindGroup);
- renderPass.draw(4);
- renderPass.end();
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
+ uBindGroup,
+ tBindGroup,
+ ]);
// Blit to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1.0);
};
diff --git a/nodes/node-layer-style/src/shared/config.ts b/nodes/node-layer-style/src/shared/config.ts
index 0dcfae5a..cab30637 100644
--- a/nodes/node-layer-style/src/shared/config.ts
+++ b/nodes/node-layer-style/src/shared/config.ts
@@ -466,70 +466,18 @@ export const LAYER_STYLE_PRESETS: LayerStylePreset[] = [
description: "Clean modern floating shadow for UI and graphics",
config: {
dropShadow: {
+ ...DEFAULT_DROP_SHADOW,
enabled: true,
- color: "#000000",
opacity: 0.35,
- angle: 120,
distance: 8,
- spread: 0,
size: 16,
- blendMode: "multiply",
- },
- innerShadow: {
- enabled: false,
- color: "#000000",
- opacity: 0.75,
- angle: 120,
- distance: 5,
- choke: 0,
- size: 5,
- blendMode: "multiply",
- },
- outerGlow: {
- enabled: false,
- color: "#ffffff",
- opacity: 0.75,
- size: 15,
- spread: 0,
- blendMode: "screen",
- },
- innerGlow: {
- enabled: false,
- color: "#ffffff",
- opacity: 0.75,
- size: 15,
- spread: 0,
- blendMode: "screen",
- },
- stroke: {
- enabled: false,
- size: 2,
- position: "outside",
- color: "#ffffff",
- opacity: 1.0,
- blendMode: "normal",
- },
- bevelEmboss: {
- enabled: false,
- style: "InnerBevel",
- technique: "Smooth",
- depth: 100,
- direction: "Up",
- size: 5,
- soften: 0,
- angle: 120,
- altitude: 30,
- highlightColor: "#ffffff",
- highlightOpacity: 0.75,
- shadowColor: "#000000",
- shadowOpacity: 0.75,
- },
- colorOverlay: {
- enabled: false,
- color: "#ff0000",
- opacity: 1.0,
- blendMode: "normal",
},
+ innerShadow: { ...DEFAULT_INNER_SHADOW },
+ outerGlow: { ...DEFAULT_GLOW },
+ innerGlow: { ...DEFAULT_GLOW },
+ stroke: { ...DEFAULT_STROKE },
+ bevelEmboss: { ...DEFAULT_BEVEL_EMBOSS },
+ colorOverlay: { ...DEFAULT_COLOR_OVERLAY },
},
},
{
@@ -538,70 +486,17 @@ export const LAYER_STYLE_PRESETS: LayerStylePreset[] = [
description: "Crisp white die-cut sticker border with contact shadow",
config: {
dropShadow: {
+ ...DEFAULT_DROP_SHADOW,
enabled: true,
- color: "#000000",
opacity: 0.4,
- angle: 120,
distance: 6,
- spread: 0,
- size: 10,
- blendMode: "multiply",
- },
- stroke: {
- enabled: true,
- size: 6,
- position: "outside",
- color: "#ffffff",
- opacity: 1.0,
- blendMode: "normal",
- },
- innerShadow: {
- enabled: false,
- color: "#000000",
- opacity: 0.75,
- angle: 120,
- distance: 5,
- choke: 0,
- size: 5,
- blendMode: "multiply",
- },
- outerGlow: {
- enabled: false,
- color: "#ffffff",
- opacity: 0.75,
- size: 15,
- spread: 0,
- blendMode: "screen",
- },
- innerGlow: {
- enabled: false,
- color: "#ffffff",
- opacity: 0.75,
- size: 15,
- spread: 0,
- blendMode: "screen",
- },
- bevelEmboss: {
- enabled: false,
- style: "InnerBevel",
- technique: "Smooth",
- depth: 100,
- direction: "Up",
- size: 5,
- soften: 0,
- angle: 120,
- altitude: 30,
- highlightColor: "#ffffff",
- highlightOpacity: 0.75,
- shadowColor: "#000000",
- shadowOpacity: 0.75,
- },
- colorOverlay: {
- enabled: false,
- color: "#ff0000",
- opacity: 1.0,
- blendMode: "normal",
},
+ stroke: { ...DEFAULT_STROKE, enabled: true, size: 6 },
+ innerShadow: { ...DEFAULT_INNER_SHADOW },
+ outerGlow: { ...DEFAULT_GLOW },
+ innerGlow: { ...DEFAULT_GLOW },
+ bevelEmboss: { ...DEFAULT_BEVEL_EMBOSS },
+ colorOverlay: { ...DEFAULT_COLOR_OVERLAY },
},
},
{
@@ -610,71 +505,33 @@ export const LAYER_STYLE_PRESETS: LayerStylePreset[] = [
description:
"Vibrant outer and inner luminescence with soft cyan radiation",
config: {
- dropShadow: {
- enabled: false,
- color: "#000000",
- opacity: 0.75,
- angle: 120,
- distance: 10,
- spread: 0,
- size: 10,
- blendMode: "multiply",
- },
- innerShadow: {
- enabled: false,
- color: "#000000",
- opacity: 0.75,
- angle: 120,
- distance: 5,
- choke: 0,
- size: 5,
- blendMode: "multiply",
- },
+ dropShadow: { ...DEFAULT_DROP_SHADOW },
+ innerShadow: { ...DEFAULT_INNER_SHADOW },
outerGlow: {
+ ...DEFAULT_GLOW,
enabled: true,
color: "#00f0ff",
opacity: 0.9,
size: 28,
spread: 12,
- blendMode: "screen",
},
innerGlow: {
+ ...DEFAULT_GLOW,
enabled: true,
- color: "#ffffff",
opacity: 0.8,
size: 10,
spread: 5,
- blendMode: "screen",
},
stroke: {
+ ...DEFAULT_STROKE,
enabled: true,
- size: 2,
position: "center",
color: "#00f0ff",
opacity: 0.9,
blendMode: "screen",
},
- bevelEmboss: {
- enabled: false,
- style: "InnerBevel",
- technique: "Smooth",
- depth: 100,
- direction: "Up",
- size: 5,
- soften: 0,
- angle: 120,
- altitude: 30,
- highlightColor: "#ffffff",
- highlightOpacity: 0.75,
- shadowColor: "#000000",
- shadowOpacity: 0.75,
- },
- colorOverlay: {
- enabled: false,
- color: "#ff0000",
- opacity: 1.0,
- blendMode: "normal",
- },
+ bevelEmboss: { ...DEFAULT_BEVEL_EMBOSS },
+ colorOverlay: { ...DEFAULT_COLOR_OVERLAY },
},
},
{
@@ -683,70 +540,33 @@ export const LAYER_STYLE_PRESETS: LayerStylePreset[] = [
description: "Pristine glass beveling with specular highlights and depth",
config: {
dropShadow: {
+ ...DEFAULT_DROP_SHADOW,
enabled: true,
- color: "#000000",
opacity: 0.25,
- angle: 120,
distance: 6,
- spread: 0,
size: 12,
- blendMode: "multiply",
},
innerShadow: {
+ ...DEFAULT_INNER_SHADOW,
enabled: true,
- color: "#000000",
opacity: 0.3,
- angle: 120,
distance: 4,
- choke: 0,
size: 6,
- blendMode: "multiply",
- },
- outerGlow: {
- enabled: false,
- color: "#ffffff",
- opacity: 0.75,
- size: 15,
- spread: 0,
- blendMode: "screen",
- },
- innerGlow: {
- enabled: true,
- color: "#ffffff",
- opacity: 0.6,
- size: 8,
- spread: 0,
- blendMode: "screen",
- },
- stroke: {
- enabled: false,
- size: 2,
- position: "outside",
- color: "#ffffff",
- opacity: 1.0,
- blendMode: "normal",
},
+ outerGlow: { ...DEFAULT_GLOW },
+ innerGlow: { ...DEFAULT_GLOW, enabled: true, opacity: 0.6, size: 8 },
+ stroke: { ...DEFAULT_STROKE },
bevelEmboss: {
+ ...DEFAULT_BEVEL_EMBOSS,
enabled: true,
- style: "InnerBevel",
- technique: "Smooth",
depth: 180,
- direction: "Up",
size: 8,
soften: 2,
- angle: 120,
altitude: 35,
- highlightColor: "#ffffff",
highlightOpacity: 0.9,
- shadowColor: "#000000",
shadowOpacity: 0.5,
},
- colorOverlay: {
- enabled: false,
- color: "#ff0000",
- opacity: 1.0,
- blendMode: "normal",
- },
+ colorOverlay: { ...DEFAULT_COLOR_OVERLAY },
},
},
{
@@ -755,70 +575,37 @@ export const LAYER_STYLE_PRESETS: LayerStylePreset[] = [
description: "Debossed letterpress engraving into paper surface",
config: {
dropShadow: {
+ ...DEFAULT_DROP_SHADOW,
enabled: true,
color: "#ffffff",
opacity: 0.85,
angle: 300,
distance: 2,
- spread: 0,
size: 1,
blendMode: "screen",
},
innerShadow: {
+ ...DEFAULT_INNER_SHADOW,
enabled: true,
- color: "#000000",
opacity: 0.7,
- angle: 120,
distance: 3,
choke: 10,
size: 4,
- blendMode: "multiply",
- },
- outerGlow: {
- enabled: false,
- color: "#ffffff",
- opacity: 0.75,
- size: 15,
- spread: 0,
- blendMode: "screen",
- },
- innerGlow: {
- enabled: false,
- color: "#ffffff",
- opacity: 0.75,
- size: 15,
- spread: 0,
- blendMode: "screen",
- },
- stroke: {
- enabled: false,
- size: 2,
- position: "outside",
- color: "#ffffff",
- opacity: 1.0,
- blendMode: "normal",
},
+ outerGlow: { ...DEFAULT_GLOW },
+ innerGlow: { ...DEFAULT_GLOW },
+ stroke: { ...DEFAULT_STROKE },
bevelEmboss: {
+ ...DEFAULT_BEVEL_EMBOSS,
enabled: true,
- style: "InnerBevel",
technique: "ChiselHard",
depth: 150,
direction: "Down",
size: 3,
- soften: 0,
- angle: 120,
- altitude: 30,
- highlightColor: "#ffffff",
highlightOpacity: 0.6,
- shadowColor: "#000000",
shadowOpacity: 0.8,
},
- colorOverlay: {
- enabled: false,
- color: "#ff0000",
- opacity: 1.0,
- blendMode: "normal",
- },
+ colorOverlay: { ...DEFAULT_COLOR_OVERLAY },
},
},
{
@@ -827,58 +614,43 @@ export const LAYER_STYLE_PRESETS: LayerStylePreset[] = [
description: "High-specular chisel gold badge with deep relief",
config: {
dropShadow: {
+ ...DEFAULT_DROP_SHADOW,
enabled: true,
- color: "#000000",
opacity: 0.6,
- angle: 120,
distance: 8,
spread: 5,
size: 12,
- blendMode: "multiply",
},
innerShadow: {
+ ...DEFAULT_INNER_SHADOW,
enabled: true,
color: "#573a08",
opacity: 0.5,
- angle: 120,
distance: 3,
- choke: 0,
- size: 5,
- blendMode: "multiply",
},
outerGlow: {
+ ...DEFAULT_GLOW,
enabled: true,
color: "#ffc83b",
opacity: 0.4,
size: 20,
spread: 5,
- blendMode: "screen",
},
innerGlow: {
+ ...DEFAULT_GLOW,
enabled: true,
color: "#fff7d1",
opacity: 0.5,
size: 6,
- spread: 0,
- blendMode: "screen",
- },
- stroke: {
- enabled: true,
- size: 2,
- position: "outside",
- color: "#b38728",
- opacity: 1.0,
- blendMode: "normal",
},
+ stroke: { ...DEFAULT_STROKE, enabled: true, color: "#b38728" },
bevelEmboss: {
+ ...DEFAULT_BEVEL_EMBOSS,
enabled: true,
- style: "InnerBevel",
technique: "ChiselHard",
depth: 350,
- direction: "Up",
size: 10,
soften: 1,
- angle: 120,
altitude: 40,
highlightColor: "#fff4cc",
highlightOpacity: 0.95,
@@ -886,6 +658,7 @@ export const LAYER_STYLE_PRESETS: LayerStylePreset[] = [
shadowOpacity: 0.85,
},
colorOverlay: {
+ ...DEFAULT_COLOR_OVERLAY,
enabled: true,
color: "#f5b324",
opacity: 0.35,
diff --git a/nodes/node-levels/src/renderers/webgpu-renderer.ts b/nodes/node-levels/src/renderers/webgpu-renderer.ts
index 37e62166..2e4478f7 100644
--- a/nodes/node-levels/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-levels/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,17 @@
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
import { colorMatrixWgsl } from "@framefields/webgpu-renderers";
-import { type LevelChannel, type LevelsOperation, defaultLevelChannel } from "../shared/config.js";
+import {
+ defaultLevelChannel,
+ type LevelChannel,
+ type LevelsOperation,
+} from "../shared/config.js";
interface DeviceLevelsResources {
colorMatrixPipeline: GPURenderPipeline;
@@ -14,48 +25,25 @@ const colorMatrixData = new Float32Array(20);
function getColorMatrixResources(device: GPUDevice, format: GPUTextureFormat) {
let res = deviceResourceCache.get(device);
if (!res) {
- const colorMatrixUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const colorMatrixUniformLayout = fragmentBindGroupLayout(device, [
+ "uniform",
+ ]);
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const singleTextureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
const colorMatrixModule = device.createShaderModule({
label: "color-matrix.wgsl",
code: colorMatrixWgsl,
});
- const colorMatrixPipeline = device.createRenderPipeline({
+ const colorMatrixPipeline = createFullscreenPipeline(device, {
label: "ColorMatrixPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [colorMatrixUniformLayout, singleTextureLayout],
- }),
- vertex: { module: colorMatrixModule, entryPoint: "vs" },
- fragment: {
- module: colorMatrixModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: colorMatrixModule,
+ layouts: [colorMatrixUniformLayout, singleTextureLayout],
+ format,
});
res = {
@@ -81,12 +69,10 @@ export const LevelsWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as unknown as
| LevelsOperation
@@ -138,36 +124,7 @@ export const LevelsWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const width = targetWidth;
const height = targetHeight;
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
- ...props,
- virtualMedia: childMedia,
- },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// Apply Color Matrix
const {
@@ -210,51 +167,18 @@ export const LevelsWebGPURenderer: WebGPUNodeRenderer = async (args) => {
targetTexture,
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline!);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline!, [
ctx.device.createBindGroup({
layout: uLayout!,
entries: [{ binding: 0, resource: { buffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout!,
tmpTex,
sampler,
),
- );
- renderPass.draw(4);
- renderPass.end();
-
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: props.opacity ?? 1 },
- );
+ ]);
- (args as any).pass = finalPass;
+ drawTextureToTarget(args, outTex, props.opacity ?? 1);
};
diff --git a/nodes/node-liquify/src/renderers/webgpu-renderer.ts b/nodes/node-liquify/src/renderers/webgpu-renderer.ts
index 17ebf82a..626edf55 100644
--- a/nodes/node-liquify/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-liquify/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,13 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ beginTargetPass,
+ createFullscreenPipeline,
+ drawChildToTexture,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ STRAIGHT_ALPHA_BLEND,
+} from "@framefields/node-sdk/gpu";
import type { LiquifyDeformOperation } from "../shared/config.js";
const MAX_OPERATIONS = 1024;
@@ -33,27 +41,7 @@ struct LiquifyUniforms {
@group(0) @binding(0) var<uniform> u : LiquifyUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
const PI = 3.141592653589793;
@@ -125,32 +113,17 @@ const deviceResourceCache = new WeakMap<GPUDevice, DeviceLiquifyResources>();
function getLiquifyResources(device: GPUDevice, format: GPUTextureFormat) {
let layouts = deviceResourceCache.get(device);
if (!layouts) {
- const uniformLayout = device.createBindGroupLayout({
- label: "LiquifyUniformLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- label: "LiquifyTextureLayout",
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(
+ device,
+ ["uniform"],
+ "LiquifyUniformLayout",
+ );
+
+ const singleTextureLayout = fragmentBindGroupLayout(
+ device,
+ ["texture", "sampler"],
+ "LiquifyTextureLayout",
+ );
layouts = {
uniformLayout,
@@ -168,34 +141,12 @@ function getLiquifyResources(device: GPUDevice, format: GPUTextureFormat) {
code: LIQUIFY_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `LiquifyPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [layouts.uniformLayout, layouts.singleTextureLayout],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "src-alpha",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [layouts.uniformLayout, layouts.singleTextureLayout],
+ format,
+ blend: STRAIGHT_ALPHA_BLEND,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -217,17 +168,7 @@ const TYPE_ENUM_MAP: Record<string, number> = {
};
export const LiquifyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
- const {
- ctx,
- encoder,
- pass,
- targetView,
- targetTexture,
- targetWidth,
- targetHeight,
- props,
- drawChild,
- } = args;
+ const { ctx, pass, targetWidth, targetHeight, props } = args;
const op = props.virtualMedia?.operation as LiquifyOp | undefined;
if (op?.op !== "Liquify" || !op) return;
@@ -243,40 +184,14 @@ export const LiquifyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const aspectRatio = height > 0 ? width / height : 1.0;
// 1. Render child media into intermediate texture
- const childTexture = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const childView = childTexture.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- childView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
+ const { texture: childTexture } = await drawChildToTexture(args, childMedia, {
+ props: {
...props,
virtualMedia: childMedia,
containerWidth: width,
containerHeight: height,
- excludeTextures: [...(props.excludeTextures || []), childTexture],
},
- childView,
- childTexture,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// 2. Prepare Uniform Buffer
const operations = op.operations || [];
@@ -321,21 +236,12 @@ export const LiquifyWebGPURenderer: WebGPUNodeRenderer = async (args) => {
);
// 3. Render final pass to targetView
- const outPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "load",
- );
+ const outPass = beginTargetPass(args);
outPass.setPipeline(resources.liquifyPipeline);
outPass.setBindGroup(0, uniformBindGroup);
outPass.setBindGroup(1, textureBindGroup);
outPass.draw(4, 1, 0, 0);
-
- args.pass = outPass;
};
export default LiquifyWebGPURenderer;
diff --git a/nodes/node-mask-math/src/index.ts b/nodes/node-mask-math/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-mask-math/src/index.ts
+++ b/nodes/node-mask-math/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-mask-math/src/renderers/webgpu-renderer.ts b/nodes/node-mask-math/src/renderers/webgpu-renderer.ts
index 6b0f4b6f..324513e1 100644
--- a/nodes/node-mask-math/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-mask-math/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,20 @@
/// <reference types="webgpu" />
import type { VirtualMediaData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ resolveHandleParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import {
MAX_RADIUS,
type MaskChannel,
@@ -78,27 +91,7 @@ struct MaskMathUniforms {
@group(2) @binding(2) var clampMinSigTex : texture_2d<f32>;
@group(2) @binding(3) var clampMaxSigTex : texture_2d<f32>;
@group(2) @binding(4) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn extractChannel(color : vec4<f32>, chCode : f32) -> f32 {
if (chCode < 0.5) {
@@ -349,70 +342,11 @@ function getMaskMathResources(
const cached = deviceResourceCache.get(device);
if (cached) return cached;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = texturesLayout(device, 3);
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalTextureLayout = texturesLayout(device, 4);
const shaderModule = device.createShaderModule({
code: WGSL_MASKMATH,
@@ -422,20 +356,10 @@ function getMaskMathResources(
bindGroupLayouts: [uniformLayout, textureLayout, signalTextureLayout],
});
- const pipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: {
- module: shaderModule,
- entryPoint: "vs",
- },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- targets: [{ format: targetFormat }],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ const pipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: pipelineLayout,
+ format: targetFormat,
});
const res: DeviceMaskMathResources = {
@@ -453,12 +377,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as MaskMathOp | undefined;
@@ -480,42 +402,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
fallbackVal: number,
clampMin = 0,
clampMax = 1000,
- ): {
- val: number;
- hasStaticSig: boolean;
- sd: MaskMathSignalData | null;
- handleId?: string | null;
- } => {
- if (handleId && op.inputs?.[handleId]?.connectionValid) {
- const outputItem = op.inputs[handleId].outputItem;
- if (outputItem?.type === "Number") {
- const numVal =
- typeof outputItem.data === "number"
- ? outputItem.data
- : ((outputItem.data as { value?: number })?.value ?? fallbackVal);
- return {
- val: Math.max(clampMin, Math.min(clampMax, numVal)),
- hasStaticSig: false,
- sd: null,
- handleId,
- };
- }
- if (outputItem?.type === "Signal" && outputItem.data) {
- return {
- val: fallbackVal,
- hasStaticSig: true,
- sd: outputItem.data as MaskMathSignalData,
- handleId,
- };
- }
- }
- return {
- val: Math.max(clampMin, Math.min(clampMax, fallbackVal)),
- hasStaticSig: false,
- sd: null,
+ ) =>
+ resolveHandleParam<MaskMathSignalData>(
+ op.inputs,
handleId,
- };
- };
+ fallbackVal,
+ clampMin,
+ clampMax,
+ );
const radiusRes = resolveField(
op.radiusHandleId,
@@ -543,77 +437,34 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
);
// 1. Render primary Mask A child media
- const tmpATex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpAView = tmpATex.createView();
-
- const childAPass = ctx.renderer.beginFrame(
- encoder,
- tmpAView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childAPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ const { texture: tmpATex, view: tmpAView } = await drawChildToTexture(
+ args,
childMedia,
{
- ...props,
- virtualMedia: childMedia,
- renderId: `${props.renderId}-maskA`,
- excludeTextures: [...(props.excludeTextures || []), tmpATex],
+ props: {
+ ...props,
+ virtualMedia: childMedia,
+ renderId: `${props.renderId}-maskA`,
+ },
},
- tmpAView,
- tmpATex,
- width,
- height,
);
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
// 2. Render optional Mask B child media if connected
let tmpBTex: GPUTexture | null = null;
if (op.maskBMedia) {
- tmpBTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- tmpATex,
- ]);
- const tmpBView = tmpBTex.createView();
-
- const childBPass = ctx.renderer.beginFrame(
- encoder,
- tmpBView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childBPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ ({ texture: tmpBTex } = await drawChildToTexture(
+ args,
op.maskBMedia as VirtualMediaData,
{
- ...props,
- virtualMedia: op.maskBMedia as VirtualMediaData,
- renderId: `${props.renderId}-maskB`,
- excludeTextures: [...(props.excludeTextures || []), tmpATex, tmpBTex],
+ props: {
+ ...props,
+ virtualMedia: op.maskBMedia as VirtualMediaData,
+ renderId: `${props.renderId}-maskB`,
+ excludeTextures: [...(props.excludeTextures || []), tmpATex],
+ },
+ exclude: [tmpATex],
},
- tmpBView,
- tmpBTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ ));
}
// 3. Compile pipeline resources
@@ -621,14 +472,17 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
getMaskMathResources(ctx.device, ctx.renderer.format);
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -637,28 +491,12 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
handleId?: string | null;
},
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- const cacheNodeId =
- res.sd.nodeId ??
- res.handleId ??
- `${props.renderId ?? "maskmath"}_${suffix}`;
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- cacheNodeId,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) =>
+ signalTextureView(
+ signalContext,
+ res,
+ res.handleId ?? `${props.renderId ?? "maskmath"}_${suffix}`,
+ );
const radiusView = getSignalView(radiusRes, "radius_sig");
const threshView = getSignalView(thresholdRes, "threshold_sig");
@@ -738,26 +576,11 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
// Pass 1: Horizontal Pass (or direct combination)
const hBuffer = ctx.renderer.getTemporaryBuffer(hUniformData);
- const hPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex1.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- hPass.setPipeline(pipeline);
- hPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex1.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [{ binding: 0, resource: { buffer: hBuffer } }],
}),
- );
- hPass.setBindGroup(
- 1,
ctx.device.createBindGroup({
layout: textureLayout,
entries: [
@@ -767,34 +590,17 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 3, resource: sampler },
],
}),
- );
- hPass.setBindGroup(2, sigBindGroup);
- hPass.draw(4);
- hPass.end();
+ sigBindGroup,
+ ]);
// Pass 2: Vertical Pass + post-processing
const vBuffer = ctx.renderer.getTemporaryBuffer(vUniformData);
- const vPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex2.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- vPass.setPipeline(pipeline);
- vPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex2.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [{ binding: 0, resource: { buffer: vBuffer } }],
}),
- );
- vPass.setBindGroup(
- 1,
ctx.device.createBindGroup({
layout: textureLayout,
entries: [
@@ -804,29 +610,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 3, resource: sampler },
],
}),
- );
- vPass.setBindGroup(2, sigBindGroup);
- vPass.draw(4);
- vPass.end();
+ sigBindGroup,
+ ]);
// 4. Final Pass: draw output into targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
const finalOpacity = props.opacity ?? op.opacity ?? 1.0;
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex2,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: finalOpacity },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex2, finalOpacity);
};
diff --git a/nodes/node-mesh-warp/src/renderers/webgpu-renderer.ts b/nodes/node-mesh-warp/src/renderers/webgpu-renderer.ts
index c5874dbf..b0cf1740 100644
--- a/nodes/node-mesh-warp/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-mesh-warp/src/renderers/webgpu-renderer.ts
@@ -1,4 +1,9 @@
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ beginTargetPass,
+ drawChildToTexture,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
import { createUniformGrid } from "../shared/utils.js";
const meshWarpWgsl = `
@@ -112,20 +117,10 @@ function getMeshWarpResources(device: GPUDevice, format: GPUTextureFormat) {
],
});
- const warpTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const warpTextureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
const module = device.createShaderModule({
label: "mesh_warp.wgsl",
@@ -174,16 +169,7 @@ function getMeshWarpResources(device: GPUDevice, format: GPUTextureFormat) {
}
export const MeshWarpWebGPURenderer: WebGPUNodeRenderer = async (args) => {
- const {
- ctx,
- props,
- drawChild,
- targetTexture,
- targetView,
- targetWidth,
- targetHeight,
- encoder,
- } = args;
+ const { ctx, props, targetWidth, targetHeight } = args;
const { virtualMedia } = props;
const op = virtualMedia?.operation as any;
if (!op || op.op !== "MeshWarp") return;
@@ -206,45 +192,15 @@ export const MeshWarpWebGPURenderer: WebGPUNodeRenderer = async (args) => {
args.pass.end();
// 1. Render child into a temporary offscreen texture at its native size
- const childTex = ctx.renderer.getTemporaryTexture(sourceWidth, sourceHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const childView = childTex.createView();
-
- const clearPass = ctx.renderer.beginFrame(
- encoder,
- childView,
- { r: 0, g: 0, b: 0, a: 0 },
- sourceWidth,
- sourceHeight,
- "clear",
- );
- clearPass.end();
-
- ctx.renderer.pushScissor({
- x: 0,
- y: 0,
+ const { texture: childTex } = await drawChildToTexture(args, childMedia, {
width: sourceWidth,
height: sourceHeight,
- });
- ctx.renderer.pushIdentity();
-
- await drawChild(
- childMedia,
- {
+ props: {
...props,
containerWidth: sourceWidth,
containerHeight: sourceHeight,
},
- childView,
- childTex,
- sourceWidth,
- sourceHeight,
- );
-
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// 2. Setup warp rendering to targetView
const cols = Number(op.cols) || 3;
@@ -300,14 +256,7 @@ export const MeshWarpWebGPURenderer: WebGPUNodeRenderer = async (args) => {
sampler,
);
- const outPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
+ const outPass = beginTargetPass(args);
outPass.setPipeline(pipeline!);
outPass.setBindGroup(0, uniformBindGroup);
@@ -316,6 +265,4 @@ export const MeshWarpWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const cellCount = (cols - 1) * (rows - 1);
const vertexCount = cellCount * 6;
outPass.draw(vertexCount);
-
- args.pass = outPass;
};
diff --git a/nodes/node-modulate/src/renderers/webgpu-renderer.ts b/nodes/node-modulate/src/renderers/webgpu-renderer.ts
index dca3aed5..44ab363f 100644
--- a/nodes/node-modulate/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-modulate/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,16 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
interface ModulateOp {
@@ -152,27 +163,7 @@ struct ModulateUniforms {
@group(2) @binding(4) var saturationTex : texture_2d<f32>;
@group(2) @binding(5) var sepiaTex : texture_2d<f32>;
@group(2) @binding(6) var sigSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn apply_exposure(c: vec4<f32>, exposure: f32) -> vec4<f32> {
let scale = pow(2.0, exposure);
@@ -309,94 +300,14 @@ fn apply_sepia(c: vec4<f32>, s: f32) -> vec4<f32> {
}
`;
-interface DeviceModulateResources {
- modulateUniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- modulateSignalsLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceModulateResources>();
const modulateData = new Float32Array(12);
-function getDeviceLayouts(device: GPUDevice): DeviceModulateResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const modulateUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const modulateSignalsLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- modulateUniformLayout,
- singleTextureLayout,
- modulateSignalsLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ modulateUniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ modulateSignalsLayout: texturesLayout(device, 6),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getModulateResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -409,22 +320,15 @@ function getModulateResources(device: GPUDevice, format: GPUTextureFormat) {
code: STATIC_MODULATE_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `ModulatePipeline_static_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.modulateUniformLayout,
- layouts.singleTextureLayout,
- layouts.modulateSignalsLayout,
- ],
- }),
- vertex: { module: modulateModule, entryPoint: "vs" },
- fragment: {
- module: modulateModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: modulateModule,
+ layouts: [
+ layouts.modulateUniformLayout,
+ layouts.singleTextureLayout,
+ layouts.modulateSignalsLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -442,12 +346,10 @@ export const ModulateWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
const compositionFrame = props.compositionFrame ?? frame;
@@ -518,37 +420,7 @@ export const ModulateWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const width = targetWidth;
const height = targetHeight;
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
- ...props,
- virtualMedia: childMedia,
- },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
const {
modulatePipeline: pipeline,
@@ -586,13 +458,7 @@ export const ModulateWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const signalViews: GPUTextureView[] = [];
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
for (const f of fields) {
const isGenerator = !!(f.info.hasSignal && f.info.sd);
@@ -620,35 +486,17 @@ export const ModulateWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const buffer = ctx.renderer.getTemporaryBuffer(modulateData);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- renderPass.setPipeline(pipeline!);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline!, [
ctx.device.createBindGroup({
layout: uLayout!,
entries: [{ binding: 0, resource: { buffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout!,
tmpTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: sigLayout!,
entries: [
@@ -661,24 +509,7 @@ export const ModulateWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 6, resource: sampler },
],
}),
- );
- renderPass.draw(4);
- renderPass.end();
-
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
+ ]);
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-noise-generator/src/index.ts b/nodes/node-noise-generator/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-noise-generator/src/index.ts
+++ b/nodes/node-noise-generator/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-noise-generator/src/renderers/webgpu-renderer.ts b/nodes/node-noise-generator/src/renderers/webgpu-renderer.ts
index 0e961cca..6171d43c 100644
--- a/nodes/node-noise-generator/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-noise-generator/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,17 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ resolveSignalField,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
interface NoiseOp {
@@ -73,27 +84,7 @@ struct NoiseUniforms {
@group(1) @binding(3) var lacunarityTex : texture_2d<f32>;
@group(1) @binding(4) var speedTex : texture_2d<f32>;
@group(1) @binding(5) var sigSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn hash3(p: vec3<f32>) -> vec3<f32> {
var p3 = fract(p * vec3<f32>(0.1031, 0.11369, 0.13787));
@@ -291,72 +282,13 @@ fn get_field(val: f32, hasSig: f32, tex: texture_2d<f32>, samp: sampler, uv: vec
}
`;
-interface DeviceNoiseResources {
- uniformLayout: GPUBindGroupLayout;
- signalsLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceNoiseResources>();
const noiseUniformData = new Float32Array(24);
-function getDeviceLayouts(device: GPUDevice): DeviceNoiseResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const signalsLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- signalsLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ signalsLayout: texturesLayout(device, 5),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getNoiseResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -369,18 +301,11 @@ function getNoiseResources(device: GPUDevice, format: GPUTextureFormat) {
code: NOISE_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `NoiseGeneratorPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [layouts.uniformLayout, layouts.signalsLayout],
- }),
- vertex: { module: noiseModule, entryPoint: "vs" },
- fragment: {
- module: noiseModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: noiseModule,
+ layouts: [layouts.uniformLayout, layouts.signalsLayout],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -410,39 +335,6 @@ function hexToRgba(hex: string): [number, number, number, number] {
return [r, g, b, a];
}
-function resolveNoiseField(
- op: NoiseOp,
- fieldName: "scale" | "octaves" | "persistence" | "lacunarity" | "speed",
- defaultValue: number,
-): { hasSignal: boolean; sd: any; value: number } {
- const handleIdKey = `${fieldName}HandleId` as keyof NoiseOp;
- const handleId = op[handleIdKey] as string | null | undefined;
- const signalInput = handleId ? op.inputs?.[handleId] : null;
-
- const hasSignal = !!(
- signalInput?.connectionValid &&
- (signalInput.outputItem?.type === "Signal" ||
- signalInput.outputItem?.type === "Numeric")
- );
- const sd = hasSignal ? signalInput?.outputItem?.data : null;
-
- let value = defaultValue;
- if (!hasSignal) {
- if (
- signalInput?.connectionValid &&
- signalInput.outputItem?.type === "Number"
- ) {
- value = Number(signalInput.outputItem.data ?? defaultValue);
- } else {
- value = Number(op[fieldName] ?? defaultValue);
- }
- } else if (sd && typeof sd === "object" && "offset" in sd) {
- value = Number(sd.offset ?? defaultValue);
- }
-
- return { hasSignal, sd, value };
-}
-
export const NoiseWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const { ctx, encoder, pass, targetWidth, targetHeight, props } = args;
@@ -459,23 +351,25 @@ export const NoiseWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const fps = props.fps || 30;
// Resolve fields & check signals
- const scaleInfo = resolveNoiseField(op, "scale", 10.0);
- const octavesInfo = resolveNoiseField(op, "octaves", 4);
- const persistenceInfo = resolveNoiseField(op, "persistence", 0.5);
- const lacunarityInfo = resolveNoiseField(op, "lacunarity", 2.0);
- const speedInfo = resolveNoiseField(op, "speed", 1.0);
+ const scaleInfo = resolveSignalField<NoiseSignalData>(op, "scale", 10.0);
+ const octavesInfo = resolveSignalField<NoiseSignalData>(op, "octaves", 4);
+ const persistenceInfo = resolveSignalField<NoiseSignalData>(
+ op,
+ "persistence",
+ 0.5,
+ );
+ const lacunarityInfo = resolveSignalField<NoiseSignalData>(
+ op,
+ "lacunarity",
+ 2.0,
+ );
+ const speedInfo = resolveSignalField<NoiseSignalData>(op, "speed", 1.0);
const noiseTypeVal =
op.noiseType === "Simplex" ? 1.0 : op.noiseType === "Voronoi" ? 2.0 : 0.0;
const outputTypeVal = op.outputType === "Video" ? 1.0 : 0.0;
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
const colorStartVal = hexToRgba(op.colorStart ?? "#000000");
const colorEndVal = hexToRgba(op.colorEnd ?? "#ffffff");
@@ -531,7 +425,7 @@ export const NoiseWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const view = signalRegistry.getOrCreate2DTextureView(
ctx.device,
encoder,
- f.info.sd.nodeId ?? `noise_${f.name}_sig`,
+ f.info.sd?.nodeId ?? `noise_${f.name}_sig`,
elapsedSeconds,
durationSeconds,
f.info.sd as NoiseSignalData,
diff --git a/nodes/node-patch-heal/src/index.ts b/nodes/node-patch-heal/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-patch-heal/src/index.ts
+++ b/nodes/node-patch-heal/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-patch-heal/src/renderers/webgpu-renderer.ts b/nodes/node-patch-heal/src/renderers/webgpu-renderer.ts
index bccca729..f974e41d 100644
--- a/nodes/node-patch-heal/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-patch-heal/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,19 @@
/// <reference types="webgpu" />
import type { SignalData, VirtualMediaData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry as globalSignalRegistry } from "@framefields/webgpu-renderers";
+import {
+ unwrapFrameParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ fragmentBindGroupLayout,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import {
MAX_FEATHER,
MAX_OFFSET,
@@ -216,85 +228,11 @@ function getDeviceResources(
let res = deviceResourceCache.get(device);
if (res) return res;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = texturesLayout(device, 2);
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 7,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 8,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalTextureLayout = texturesLayout(device, 8);
const shaderModule = device.createShaderModule({
code: `${WGSL_FULLSCREEN_VS}\n${WGSL_PATCHHEAL_FS}`,
@@ -304,31 +242,16 @@ function getDeviceResources(
bindGroupLayouts: [uniformLayout, textureLayout, signalTextureLayout],
});
- const pipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: { module: shaderModule, entryPoint: "vs_main" },
- fragment: {
- module: shaderModule,
- entryPoint: "fs_main",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "zero",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "zero",
- operation: "add",
- },
- },
- },
- ],
+ const pipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: pipelineLayout,
+ format,
+ blend: {
+ color: { srcFactor: "one", dstFactor: "zero", operation: "add" },
+ alpha: { srcFactor: "one", dstFactor: "zero", operation: "add" },
},
- primitive: { topology: "triangle-strip" },
+ vertexEntryPoint: "vs_main",
+ fragmentEntryPoint: "fs_main",
});
res = {
@@ -360,12 +283,10 @@ export const PatchHealWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as unknown as
@@ -385,80 +306,31 @@ export const PatchHealWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const fps = props.fps || 30;
const nodeId = op.inputs ? Object.keys(op.inputs)[0] : "patch_heal_node";
- const signalRegistry = globalSignalRegistry;
-
// 1. Draw source child media into tmpSrcTex
- const tmpSrcTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpSrcView = tmpSrcTex.createView();
-
- const srcClearPass = ctx.renderer.beginFrame(
- encoder,
- tmpSrcView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- srcClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ const { texture: tmpSrcTex, view: tmpSrcView } = await drawChildToTexture(
+ args,
childMedia,
{
- ...props,
- virtualMedia: childMedia,
- renderId: `${props.renderId}-src`,
- excludeTextures: [...(props.excludeTextures || []), tmpSrcTex],
+ props: {
+ ...props,
+ virtualMedia: childMedia,
+ renderId: `${props.renderId}-src`,
+ },
},
- tmpSrcView,
- tmpSrcTex,
- width,
- height,
);
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
// 2. Draw optional mask child media into maskTex if present
let maskTex: GPUTexture | null = null;
if (op.maskMedia) {
- maskTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- tmpSrcTex,
- ]);
- const maskView = maskTex.createView();
-
- const maskClearPass = ctx.renderer.beginFrame(
- encoder,
- maskView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- maskClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- op.maskMedia,
- {
+ ({ texture: maskTex } = await drawChildToTexture(args, op.maskMedia, {
+ props: {
...props,
virtualMedia: op.maskMedia,
renderId: `${props.renderId}-mask`,
- excludeTextures: [...(props.excludeTextures || []), tmpSrcTex, maskTex],
+ excludeTextures: [...(props.excludeTextures || []), tmpSrcTex],
},
- maskView,
- maskTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ exclude: [tmpSrcTex],
+ }));
}
// 3. Compile pipeline resources
@@ -471,13 +343,7 @@ export const PatchHealWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
const patches = normalizePatches(op);
@@ -526,28 +392,11 @@ export const PatchHealWebGPURenderer: WebGPUNodeRenderer = async (args) => {
fallbackVal);
val = Math.max(minVal, Math.min(maxVal, Number(numVal)));
} else {
- let rawVal = fallbackVal as unknown;
- if (rawVal !== undefined && rawVal !== null) {
- if (typeof rawVal === "object") {
- if (
- "get" in (rawVal as Record<string, unknown>) &&
- typeof (rawVal as Record<string, unknown>).get === "function"
- ) {
- rawVal = (
- rawVal as {
- get: (ctx: { frame: number; fps: number }) => unknown;
- }
- ).get({ frame: compositionFrame, fps });
- } else if ("value" in (rawVal as Record<string, unknown>)) {
- rawVal = (rawVal as { value: unknown }).value;
- }
- } else if (typeof rawVal === "function") {
- rawVal = (rawVal as (f: number, fps: number) => unknown)(
- compositionFrame,
- fps,
- );
- }
- }
+ const rawVal = unwrapFrameParam(
+ fallbackVal,
+ compositionFrame,
+ fps,
+ ).value;
const numVal = Number(rawVal ?? fallbackVal);
val = Math.max(
minVal,
@@ -622,6 +471,15 @@ export const PatchHealWebGPURenderer: WebGPUNodeRenderer = async (args) => {
? resolveBindable("opacity", patch.opacity, 0.0, 1.0)
: { val: patch.opacity, hasStaticSig: false, sd: null };
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -629,26 +487,7 @@ export const PatchHealWebGPURenderer: WebGPUNodeRenderer = async (args) => {
sd: PatchHealSignalData | null;
},
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd && signalRegistry) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry
- ? signalRegistry.getDummy1x1TextureView(ctx.device)
- : tmpSrcView;
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const offsetXView = getSignalView(offsetXRes, "offsetX_sig");
const offsetYView = getSignalView(offsetYRes, "offsetY_sig");
@@ -720,49 +559,18 @@ export const PatchHealWebGPURenderer: WebGPUNodeRenderer = async (args) => {
],
});
- const healPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: destinationTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- healPass.setPipeline(pipeline);
- healPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, destinationTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uBuffer } }],
}),
- );
- healPass.setBindGroup(1, texBindGroup);
- healPass.setBindGroup(2, sigBindGroup);
- healPass.draw(4);
- healPass.end();
+ texBindGroup,
+ sigBindGroup,
+ ]);
currentInputTex = destinationTex;
}
// Final blit to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- currentInputTex,
- { x: 0, y: 0, width, height },
- { opacity: 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, currentInputTex);
};
diff --git a/nodes/node-procedural-vfx/src/index.ts b/nodes/node-procedural-vfx/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-procedural-vfx/src/index.ts
+++ b/nodes/node-procedural-vfx/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-procedural-vfx/src/renderers/webgpu-renderer.ts b/nodes/node-procedural-vfx/src/renderers/webgpu-renderer.ts
index 040a13f0..ff627ade 100644
--- a/nodes/node-procedural-vfx/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-procedural-vfx/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,17 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ resolveSignalField,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { signalRegistry } from "@framefields/webgpu-renderers";
interface ProceduralVFXYOp {
@@ -73,27 +84,7 @@ struct VFXUniforms {
@group(1) @binding(3) var intensityTex : texture_2d<f32>;
@group(1) @binding(4) var seedTex : texture_2d<f32>;
@group(1) @binding(5) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
// ---- hashing & noise primitives -------------------------------------------
fn hash21(p: vec2<f32>) -> vec2<f32> {
@@ -393,72 +384,13 @@ fn fxEnergyBeam(p: vec2<f32>, time: f32, scl: f32, dens: f32, inten: f32, cA: ve
}
`;
-interface DeviceVFXResources {
- uniformLayout: GPUBindGroupLayout;
- signalsLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceVFXResources>();
const vfxUniformData = new Float32Array(24);
-function getDeviceLayouts(device: GPUDevice): DeviceVFXResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const signalsLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- signalsLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ signalsLayout: texturesLayout(device, 5),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getVFXResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -471,18 +403,11 @@ function getVFXResources(device: GPUDevice, format: GPUTextureFormat) {
code: VFX_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `ProceduralVFXPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [layouts.uniformLayout, layouts.signalsLayout],
- }),
- vertex: { module: vfxModule, entryPoint: "vs" },
- fragment: {
- module: vfxModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: vfxModule,
+ layouts: [layouts.uniformLayout, layouts.signalsLayout],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -526,39 +451,6 @@ const VFX_EFFECT_INDEX: Record<string, number> = {
EnergyBeam: 10,
};
-function resolveVFXField(
- op: ProceduralVFXYOp,
- fieldName: "density" | "scale" | "speed" | "intensity" | "seed",
- defaultValue: number,
-): { hasSignal: boolean; sd: any; value: number } {
- const handleIdKey = `${fieldName}HandleId` as keyof ProceduralVFXYOp;
- const handleId = op[handleIdKey] as string | null | undefined;
- const signalInput = handleId ? op.inputs?.[handleId] : null;
-
- const hasSignal = !!(
- signalInput?.connectionValid &&
- (signalInput.outputItem?.type === "Signal" ||
- signalInput.outputItem?.type === "Numeric")
- );
- const sd = hasSignal ? signalInput?.outputItem?.data : null;
-
- let value = defaultValue;
- if (!hasSignal) {
- if (
- signalInput?.connectionValid &&
- signalInput.outputItem?.type === "Number"
- ) {
- value = Number(signalInput.outputItem.data ?? defaultValue);
- } else {
- value = Number(op[fieldName] ?? defaultValue);
- }
- } else if (sd && typeof sd === "object" && "offset" in sd) {
- value = Number(sd.offset ?? defaultValue);
- }
-
- return { hasSignal, sd, value };
-}
-
export const ProceduralVFXWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const { ctx, encoder, pass, targetWidth, targetHeight, props } = args;
@@ -574,22 +466,16 @@ export const ProceduralVFXWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const frame = props.frame ?? 0;
const fps = props.fps || 30;
- const densityInfo = resolveVFXField(op, "density", 0.6);
- const scaleInfo = resolveVFXField(op, "scale", 0.01);
- const speedInfo = resolveVFXField(op, "speed", 1.0);
- const intensityInfo = resolveVFXField(op, "intensity", 0.8);
- const seedInfo = resolveVFXField(op, "seed", 1234);
+ const densityInfo = resolveSignalField<VFXSignalData>(op, "density", 0.6);
+ const scaleInfo = resolveSignalField<VFXSignalData>(op, "scale", 0.01);
+ const speedInfo = resolveSignalField<VFXSignalData>(op, "speed", 1.0);
+ const intensityInfo = resolveSignalField<VFXSignalData>(op, "intensity", 0.8);
+ const seedInfo = resolveSignalField<VFXSignalData>(op, "seed", 1234);
const effectIndex = VFX_EFFECT_INDEX[op.effectType ?? "Smoke"] ?? 0;
const outputTypeVal = op.outputType === "Video" ? 1.0 : 0.0;
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
const colorStartVal = hexToRgba(op.colorStart ?? "#ffffff");
const colorEndVal = hexToRgba(op.colorEnd ?? "#ff5500");
@@ -645,7 +531,7 @@ export const ProceduralVFXWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const view = signalRegistry.getOrCreate2DTextureView(
ctx.device,
encoder,
- f.info.sd.nodeId ?? `procedural_vfx_${f.name}_sig`,
+ f.info.sd?.nodeId ?? `procedural_vfx_${f.name}_sig`,
elapsedSeconds,
durationSeconds,
f.info.sd as VFXSignalData,
diff --git a/nodes/node-refine-edge/src/index.ts b/nodes/node-refine-edge/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-refine-edge/src/index.ts
+++ b/nodes/node-refine-edge/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-refine-edge/src/renderers/webgpu-renderer.ts b/nodes/node-refine-edge/src/renderers/webgpu-renderer.ts
index c5bf5d4b..dc14f148 100644
--- a/nodes/node-refine-edge/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-refine-edge/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,20 @@
/// <reference types="webgpu" />
import type { VirtualMediaData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ resolveHandleParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import {
MAX_DECONTAMINATE,
MAX_FEATHER,
@@ -74,27 +87,7 @@ struct RefineEdgeUniforms {
@group(2) @binding(3) var featherSigTex : texture_2d<f32>;
@group(2) @binding(4) var shiftEdgeSigTex : texture_2d<f32>;
@group(2) @binding(5) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn extractChannel(color : vec4<f32>, chCode : f32) -> f32 {
if (chCode < 0.5) {
@@ -342,75 +335,11 @@ function getRefineEdgeResources(
const cached = deviceResourceCache.get(device);
if (cached) return cached;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = texturesLayout(device, 3);
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalTextureLayout = texturesLayout(device, 5);
const shaderModule = device.createShaderModule({
code: WGSL_REFINE_EDGE,
@@ -420,20 +349,10 @@ function getRefineEdgeResources(
bindGroupLayouts: [uniformLayout, textureLayout, signalTextureLayout],
});
- const pipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: {
- module: shaderModule,
- entryPoint: "vs",
- },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- targets: [{ format: targetFormat }],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ const pipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: pipelineLayout,
+ format: targetFormat,
});
const res: DeviceRefineEdgeResources = {
@@ -451,12 +370,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as RefineEdgeOp | undefined;
@@ -478,42 +395,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
fallbackVal: number,
clampMin = 0,
clampMax = 1000,
- ): {
- val: number;
- hasStaticSig: boolean;
- sd: RefineEdgeSignalData | null;
- handleId?: string | null;
- } => {
- if (handleId && op.inputs?.[handleId]?.connectionValid) {
- const outputItem = op.inputs[handleId].outputItem;
- if (outputItem?.type === "Number") {
- const numVal =
- typeof outputItem.data === "number"
- ? outputItem.data
- : ((outputItem.data as { value?: number })?.value ?? fallbackVal);
- return {
- val: Math.max(clampMin, Math.min(clampMax, numVal)),
- hasStaticSig: false,
- sd: null,
- handleId,
- };
- }
- if (outputItem?.type === "Signal" && outputItem.data) {
- return {
- val: fallbackVal,
- hasStaticSig: true,
- sd: outputItem.data as RefineEdgeSignalData,
- handleId,
- };
- }
- }
- return {
- val: Math.max(clampMin, Math.min(clampMax, fallbackVal)),
- hasStaticSig: false,
- sd: null,
+ ) =>
+ resolveHandleParam<RefineEdgeSignalData>(
+ op.inputs,
handleId,
- };
- };
+ fallbackVal,
+ clampMin,
+ clampMax,
+ );
const decontamRes = resolveField(
op.decontaminateAmountHandleId,
@@ -547,81 +436,34 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
);
// 1. Render primary Input child media
- const tmpInputTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpInputView = tmpInputTex.createView();
-
- const childInputPass = ctx.renderer.beginFrame(
- encoder,
- tmpInputView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childInputPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ const { texture: tmpInputTex, view: tmpInputView } = await drawChildToTexture(
+ args,
childMedia,
{
- ...props,
- virtualMedia: childMedia,
- renderId: `${props.renderId}-input`,
- excludeTextures: [...(props.excludeTextures || []), tmpInputTex],
+ props: {
+ ...props,
+ virtualMedia: childMedia,
+ renderId: `${props.renderId}-input`,
+ },
},
- tmpInputView,
- tmpInputTex,
- width,
- height,
);
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
// 2. Render optional Matte child media if connected
let tmpMatteTex: GPUTexture | null = null;
if (op.matteMedia) {
- tmpMatteTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- tmpInputTex,
- ]);
- const tmpMatteView = tmpMatteTex.createView();
-
- const childMattePass = ctx.renderer.beginFrame(
- encoder,
- tmpMatteView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childMattePass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
+ ({ texture: tmpMatteTex } = await drawChildToTexture(
+ args,
op.matteMedia as VirtualMediaData,
{
- ...props,
- virtualMedia: op.matteMedia as VirtualMediaData,
- renderId: `${props.renderId}-matte`,
- excludeTextures: [
- ...(props.excludeTextures || []),
- tmpInputTex,
- tmpMatteTex,
- ],
+ props: {
+ ...props,
+ virtualMedia: op.matteMedia as VirtualMediaData,
+ renderId: `${props.renderId}-matte`,
+ excludeTextures: [...(props.excludeTextures || []), tmpInputTex],
+ },
+ exclude: [tmpInputTex],
},
- tmpMatteView,
- tmpMatteTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ ));
}
// 3. Compile pipeline resources
@@ -629,14 +471,17 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
getRefineEdgeResources(ctx.device, ctx.renderer.format);
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -645,28 +490,12 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
handleId?: string | null;
},
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- const cacheNodeId =
- res.sd.nodeId ??
- res.handleId ??
- `${props.renderId ?? "refineedge"}_${suffix}`;
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- cacheNodeId,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) =>
+ signalTextureView(
+ signalContext,
+ res,
+ res.handleId ?? `${props.renderId ?? "refineedge"}_${suffix}`,
+ );
const decontamView = getSignalView(decontamRes, "decontam_sig");
const radiusView = getSignalView(radiusRes, "radius_sig");
@@ -745,26 +574,11 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
// Pass 1: Horizontal Pass
const hBuffer = ctx.renderer.getTemporaryBuffer(hUniformData);
- const hPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex1.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- hPass.setPipeline(pipeline);
- hPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex1.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [{ binding: 0, resource: { buffer: hBuffer } }],
}),
- );
- hPass.setBindGroup(
- 1,
ctx.device.createBindGroup({
layout: textureLayout,
entries: [
@@ -774,34 +588,17 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 3, resource: sampler },
],
}),
- );
- hPass.setBindGroup(2, sigBindGroup);
- hPass.draw(4);
- hPass.end();
+ sigBindGroup,
+ ]);
// Pass 2: Vertical Pass + Defringe & Composite
const vBuffer = ctx.renderer.getTemporaryBuffer(vUniformData);
- const vPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex2.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- vPass.setPipeline(pipeline);
- vPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex2.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [{ binding: 0, resource: { buffer: vBuffer } }],
}),
- );
- vPass.setBindGroup(
- 1,
ctx.device.createBindGroup({
layout: textureLayout,
entries: [
@@ -811,29 +608,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 3, resource: sampler },
],
}),
- );
- vPass.setBindGroup(2, sigBindGroup);
- vPass.draw(4);
- vPass.end();
+ sigBindGroup,
+ ]);
// 4. Final Pass: draw output into targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
const finalOpacity = props.opacity ?? op.opacity ?? 1.0;
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex2,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: finalOpacity },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex2, finalOpacity);
};
diff --git a/nodes/node-refraction-caustics/src/index.ts b/nodes/node-refraction-caustics/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-refraction-caustics/src/index.ts
+++ b/nodes/node-refraction-caustics/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-refraction-caustics/src/renderers/webgpu-renderer.ts b/nodes/node-refraction-caustics/src/renderers/webgpu-renderer.ts
index 8cdad7a3..069eac45 100644
--- a/nodes/node-refraction-caustics/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-refraction-caustics/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,19 @@
/// <reference types="webgpu" />
import type { SignalData, VirtualMediaData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ resolveSignalField,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { parseColor, signalRegistry } from "@framefields/webgpu-renderers";
interface RefractionCaustics3DOp {
@@ -122,27 +135,7 @@ struct RefractionCausticsUniforms {
@group(3) @binding(6) var lightPosXSigTex : texture_2d<f32>;
@group(3) @binding(7) var lightPosYSigTex : texture_2d<f32>;
@group(3) @binding(8) var sigSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn sampleDepth(uv: vec2<f32>) -> f32 {
let clampedUV = clamp(uv, vec2<f32>(0.0), vec2<f32>(1.0));
@@ -438,95 +431,19 @@ function getDeviceLayouts(
let res = deviceResourceCache.get(device);
if (res) return res;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const srcTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const srcTextureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
- const depthTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const depthTextureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 7,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 8,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalTextureLayout = texturesLayout(device, 8);
res = {
uniformLayout,
@@ -553,23 +470,16 @@ function getRefractionCausticsResources(
code: REFRACTION_CAUSTICS_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `RefractionCausticsPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.srcTextureLayout,
- layouts.depthTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.srcTextureLayout,
+ layouts.depthTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
@@ -584,60 +494,6 @@ function getRefractionCausticsResources(
};
}
-function resolveSignalField(
- op: RefractionCaustics3DOp,
- fieldName:
- | "ior"
- | "dispersion"
- | "refractionScale"
- | "causticBrightness"
- | "causticScale"
- | "fluidRipples"
- | "roughness"
- | "lightPosX"
- | "lightPosY"
- | "specularIntensity"
- | "depthScale"
- | "tintStrength"
- | "rippleSpeed",
- defaultValue: number,
- minVal: number,
- maxVal: number,
-): { hasSignal: boolean; sd: RefractionSignalData | null; value: number } {
- const handleIdKey = `${fieldName}HandleId` as keyof RefractionCaustics3DOp;
- const handleId = op[handleIdKey] as string | null | undefined;
- const signalInput = handleId ? op.inputs?.[handleId] : null;
-
- const hasSignal = !!(
- signalInput?.connectionValid &&
- (signalInput.outputItem?.type === "Signal" ||
- signalInput.outputItem?.type === "Numeric")
- );
- const sd =
- hasSignal && signalInput?.outputItem?.data
- ? (signalInput.outputItem.data as RefractionSignalData)
- : null;
-
- let value = defaultValue;
- if (!hasSignal) {
- if (
- signalInput?.connectionValid &&
- signalInput.outputItem?.type === "Number"
- ) {
- const raw = Number(signalInput.outputItem.data ?? defaultValue);
- value = Math.max(minVal, Math.min(maxVal, raw));
- } else {
- const raw = Number(op[fieldName] ?? defaultValue);
- value = Math.max(minVal, Math.min(maxVal, raw));
- }
- } else if (sd) {
- const raw = Number(sd.offset ?? defaultValue);
- value = Math.max(minVal, Math.min(maxVal, raw));
- }
-
- return { hasSignal, sd, value };
-}
-
export const RefractionCaustics3DWebGPURenderer: WebGPUNodeRenderer = async (
args,
) => {
@@ -645,12 +501,10 @@ export const RefractionCaustics3DWebGPURenderer: WebGPUNodeRenderer = async (
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -670,39 +524,14 @@ export const RefractionCaustics3DWebGPURenderer: WebGPUNodeRenderer = async (
const height = targetHeight;
// 1. Draw input source child to temporary texture
- const srcTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const srcView = srcTex.createView();
-
- const srcClearPass = ctx.renderer.beginFrame(
- encoder,
- srcView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- srcClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- sourceMedia,
- {
+ const { texture: srcTex } = await drawChildToTexture(args, sourceMedia, {
+ props: {
...props,
virtualMedia: sourceMedia,
containerWidth: width,
containerHeight: height,
},
- srcView,
- srcTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// End active pass from child drawing
args.pass.end();
@@ -710,51 +539,24 @@ export const RefractionCaustics3DWebGPURenderer: WebGPUNodeRenderer = async (
// 2. Draw depth map if provided
const depthMedia = op.depthMedia;
let depthTex: GPUTexture;
- let depthView: GPUTextureView;
let hasDepthMap = false;
if (depthMedia) {
- depthTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- srcTex,
- ]);
- depthView = depthTex.createView();
-
- const depthClearPass = ctx.renderer.beginFrame(
- encoder,
- depthView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- depthClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- depthMedia,
- {
+ ({ texture: depthTex } = await drawChildToTexture(args, depthMedia, {
+ props: {
...props,
virtualMedia: depthMedia,
containerWidth: width,
containerHeight: height,
},
- depthView,
- depthTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ exclude: [srcTex],
+ }));
// End active pass from depth drawing
args.pass.end();
hasDepthMap = true;
} else {
depthTex = srcTex;
- depthView = srcView;
hasDepthMap = false;
}
@@ -770,62 +572,109 @@ export const RefractionCaustics3DWebGPURenderer: WebGPUNodeRenderer = async (
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
// 4. Resolve timing and signals
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 4.0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(
+ props,
+ frame,
+ fps,
+ 4.0,
+ );
const timeProgress =
durationSeconds > 0
? (elapsedSeconds % durationSeconds) / durationSeconds
: 0.0;
- const iorInfo = resolveSignalField(op, "ior", 1.52, 1.0, 2.5);
- const dispersionInfo = resolveSignalField(op, "dispersion", 0.04, 0.0, 0.2);
- const refractScaleInfo = resolveSignalField(
+ const iorInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "ior",
+ 1.52,
+ 1.0,
+ 2.5,
+ );
+ const dispersionInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "dispersion",
+ 0.04,
+ 0.0,
+ 0.2,
+ );
+ const refractScaleInfo = resolveSignalField<RefractionSignalData>(
op,
"refractionScale",
0.5,
0.0,
2.0,
);
- const causticInfo = resolveSignalField(
+ const causticInfo = resolveSignalField<RefractionSignalData>(
op,
"causticBrightness",
0.6,
0.0,
2.0,
);
- const causticScaleInfo = resolveSignalField(
+ const causticScaleInfo = resolveSignalField<RefractionSignalData>(
op,
"causticScale",
1.0,
0.1,
5.0,
);
- const ripplesInfo = resolveSignalField(op, "fluidRipples", 0.0, 0.0, 1.0);
- const roughnessInfo = resolveSignalField(op, "roughness", 0.0, 0.0, 1.0);
- const lightPosXInfo = resolveSignalField(op, "lightPosX", 0.5, 0.0, 1.0);
- const lightPosYInfo = resolveSignalField(op, "lightPosY", 0.2, 0.0, 1.0);
- const specularInfo = resolveSignalField(
+ const ripplesInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "fluidRipples",
+ 0.0,
+ 0.0,
+ 1.0,
+ );
+ const roughnessInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "roughness",
+ 0.0,
+ 0.0,
+ 1.0,
+ );
+ const lightPosXInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "lightPosX",
+ 0.5,
+ 0.0,
+ 1.0,
+ );
+ const lightPosYInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "lightPosY",
+ 0.2,
+ 0.0,
+ 1.0,
+ );
+ const specularInfo = resolveSignalField<RefractionSignalData>(
op,
"specularIntensity",
0.8,
0.0,
2.0,
);
- const depthScaleInfo = resolveSignalField(op, "depthScale", 1.0, 0.1, 5.0);
- const tintStrengthInfo = resolveSignalField(
+ const depthScaleInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "depthScale",
+ 1.0,
+ 0.1,
+ 5.0,
+ );
+ const tintStrengthInfo = resolveSignalField<RefractionSignalData>(
op,
"tintStrength",
0.0,
0.0,
1.0,
);
- const rippleSpeedInfo = resolveSignalField(op, "rippleSpeed", 1.0, 0.0, 5.0);
+ const rippleSpeedInfo = resolveSignalField<RefractionSignalData>(
+ op,
+ "rippleSpeed",
+ 1.0,
+ 0.0,
+ 5.0,
+ );
const getSigTextureView = (
info: { hasSignal: boolean; sd: RefractionSignalData | null },
@@ -929,45 +778,23 @@ export const RefractionCaustics3DWebGPURenderer: WebGPUNodeRenderer = async (
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
srcLayout,
srcTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 2,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
depthLayout,
depthTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 3,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -982,26 +809,8 @@ export const RefractionCaustics3DWebGPURenderer: WebGPUNodeRenderer = async (
{ binding: 8, resource: sampler },
],
}),
- );
-
- renderPass.draw(4);
- renderPass.end();
+ ]);
// 7. Final Pass to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-relight-3d/src/index.ts b/nodes/node-relight-3d/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-relight-3d/src/index.ts
+++ b/nodes/node-relight-3d/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-relight-3d/src/renderers/webgpu-renderer.ts b/nodes/node-relight-3d/src/renderers/webgpu-renderer.ts
index 5dcf9045..94f89fc3 100644
--- a/nodes/node-relight-3d/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-relight-3d/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,20 @@
/// <reference types="webgpu" />
+
import type { SignalData, VirtualMediaData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ resolveFrameNumber,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { parseColor, signalRegistry } from "@framefields/webgpu-renderers";
interface Relight3DOp {
@@ -112,27 +126,7 @@ struct RelightUniforms {
@group(3) @binding(6) var specularSigTex : texture_2d<f32>;
@group(3) @binding(7) var volumetricSigTex : texture_2d<f32>;
@group(3) @binding(8) var sigSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn sampleDepth(uv: vec2<f32>) -> f32 {
var raw: f32 = 0.0;
@@ -290,120 +284,13 @@ fn computeNormal(uv: vec2<f32>, texelSize: vec2<f32>) -> vec3<f32> {
}
`;
-interface DeviceRelightResources {
- uniformLayout: GPUBindGroupLayout;
- srcTextureLayout: GPUBindGroupLayout;
- depthTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceRelightResources>();
-
-function getDeviceLayouts(device: GPUDevice): DeviceRelightResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const srcTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const depthTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 7,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 8,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- srcTextureLayout,
- depthTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ srcTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ depthTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: texturesLayout(device, 8),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getRelightResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -416,23 +303,16 @@ function getRelightResources(device: GPUDevice, format: GPUTextureFormat) {
code: RELIGHT_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `RelightPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.srcTextureLayout,
- layouts.depthTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.srcTextureLayout,
+ layouts.depthTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
@@ -447,52 +327,6 @@ function getRelightResources(device: GPUDevice, format: GPUTextureFormat) {
};
}
-function resolveVal(
- val: unknown,
- fallback: number,
- frame: number,
- fps: number,
-): number {
- if (val === undefined || val === null) return fallback;
- if (typeof val === "number") return Number.isFinite(val) ? val : fallback;
- if (typeof val === "object" && val !== null) {
- if (
- "get" in (val as Record<string, unknown>) &&
- typeof (val as { get: unknown }).get === "function"
- ) {
- try {
- const res = (val as { get: (ctx?: unknown) => unknown }).get({
- frame,
- fps,
- time: fps > 0 ? frame / fps : 0,
- duration: 0,
- durationMs: 0,
- progress: 0,
- deltaTime: fps > 0 ? 1 / fps : 0,
- });
- const n = Number(res);
- if (Number.isFinite(n)) return n;
- } catch {
- // fallback
- }
- }
- if ("value" in (val as Record<string, unknown>)) {
- const n = Number((val as { value: unknown }).value);
- if (Number.isFinite(n)) return n;
- }
- }
- if (typeof val === "function") {
- try {
- const n = Number(val({ frame, fps, time: fps > 0 ? frame / fps : 0 }));
- if (Number.isFinite(n)) return n;
- } catch {
- // fallback
- }
- }
- const n = Number(val);
- return Number.isFinite(n) ? n : fallback;
-}
-
function resolveSignalField(
op: Relight3DOp,
fieldName:
@@ -534,7 +368,7 @@ function resolveSignalField(
signalInput?.connectionValid &&
signalInput.outputItem?.type === "Number"
) {
- const raw = resolveVal(
+ const raw = resolveFrameNumber(
signalInput.outputItem.data,
defaultValue,
frame,
@@ -542,11 +376,11 @@ function resolveSignalField(
);
value = Math.max(minVal, Math.min(maxVal, raw));
} else {
- const raw = resolveVal(op[fieldName], defaultValue, frame, fps);
+ const raw = resolveFrameNumber(op[fieldName], defaultValue, frame, fps);
value = Math.max(minVal, Math.min(maxVal, raw));
}
} else if (sd) {
- const raw = resolveVal(sd.offset, defaultValue, frame, fps);
+ const raw = resolveFrameNumber(sd.offset, defaultValue, frame, fps);
value = Math.max(minVal, Math.min(maxVal, raw));
}
@@ -558,12 +392,10 @@ export const Relight3DWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -582,39 +414,14 @@ export const Relight3DWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const height = targetHeight;
// 1. Draw input source child to temporary texture
- const srcTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const srcView = srcTex.createView();
-
- const srcClearPass = ctx.renderer.beginFrame(
- encoder,
- srcView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- srcClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- sourceMedia,
- {
+ const { texture: srcTex } = await drawChildToTexture(args, sourceMedia, {
+ props: {
...props,
virtualMedia: sourceMedia,
containerWidth: width,
containerHeight: height,
},
- srcView,
- srcTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// End active pass from child drawing
args.pass.end();
@@ -627,52 +434,25 @@ export const Relight3DWebGPURenderer: WebGPUNodeRenderer = async (args) => {
: null);
const depthMedia = normalMedia ?? op.depthMedia;
let depthTex: GPUTexture;
- let depthView: GPUTextureView;
let hasDepthMap = false;
const hasNormalMap = Boolean(normalMedia);
if (depthMedia) {
- depthTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- srcTex,
- ]);
- depthView = depthTex.createView();
-
- const depthClearPass = ctx.renderer.beginFrame(
- encoder,
- depthView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- depthClearPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- depthMedia,
- {
+ ({ texture: depthTex } = await drawChildToTexture(args, depthMedia, {
+ props: {
...props,
virtualMedia: depthMedia,
containerWidth: width,
containerHeight: height,
},
- depthView,
- depthTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ exclude: [srcTex],
+ }));
// End active pass from depth drawing
args.pass.end();
hasDepthMap = true;
} else {
depthTex = srcTex;
- depthView = srcView;
hasDepthMap = false;
}
@@ -908,45 +688,23 @@ export const Relight3DWebGPURenderer: WebGPUNodeRenderer = async (args) => {
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
srcLayout,
srcTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 2,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
depthLayout,
depthTex,
sampler,
),
- );
- renderPass.setBindGroup(
- 3,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -961,26 +719,8 @@ export const Relight3DWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 8, resource: sampler },
],
}),
- );
-
- renderPass.draw(4);
- renderPass.end();
+ ]);
// 7. Final Pass to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1);
};
diff --git a/nodes/node-resizer-scaler/src/renderers/webgpu-renderer.ts b/nodes/node-resizer-scaler/src/renderers/webgpu-renderer.ts
index ceece7db..e9d1aae3 100644
--- a/nodes/node-resizer-scaler/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-resizer-scaler/src/renderers/webgpu-renderer.ts
@@ -2,6 +2,14 @@
import { getActiveMediaMetadata } from "@framefields/core";
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ beginTargetPass,
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
interface ResizerScalerOp {
op: "ResizerScaler";
@@ -41,27 +49,7 @@ struct ScalerUniforms {
@group(0) @binding(0) var<uniform> u : ScalerUniforms;
@group(0) @binding(1) var s : sampler;
@group(0) @binding(2) var t_src : texture_2d<f32>;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let uv = in.uv;
@@ -161,25 +149,11 @@ function getDeviceLayouts(device: GPUDevice): DeviceScalerResources {
let res = deviceResourceCache.get(device);
if (res) return res;
- const scalerUniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- ],
- });
+ const scalerUniformLayout = fragmentBindGroupLayout(device, [
+ "uniform",
+ "sampler",
+ "texture",
+ ]);
res = {
scalerUniformLayout,
@@ -201,18 +175,11 @@ function getScalerResources(device: GPUDevice, format: GPUTextureFormat) {
code: RESIZER_SCALER_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `ResizerScalerPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [layouts.scalerUniformLayout],
- }),
- vertex: { module: scalerModule, entryPoint: "vs" },
- fragment: {
- module: scalerModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: scalerModule,
+ layouts: [layouts.scalerUniformLayout],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -250,17 +217,8 @@ function parseHexToRGBA(hex: string): [number, number, number, number] {
const scalerData = new Float32Array(24);
export const ResizerScalerWebGPURenderer: WebGPUNodeRenderer = async (args) => {
- const {
- ctx,
- encoder,
- pass,
- targetView,
- targetTexture,
- targetWidth,
- targetHeight,
- props,
- drawChild,
- } = args;
+ const { ctx, encoder, pass, targetView, targetWidth, targetHeight, props } =
+ args;
const op = props.virtualMedia?.operation as ResizerScalerOp | undefined;
if (op?.op !== "ResizerScaler" || !op) return;
@@ -276,41 +234,16 @@ export const ResizerScalerWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const srcHeight = childMeta?.height || targetHeight || 1080;
// Get a temporary texture representing the native child size to draw it into
- const tmpTex = ctx.renderer.getTemporaryTexture(srcWidth, srcHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- // Clear temporary texture frame
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- srcWidth,
- srcHeight,
- "clear",
- );
- childPass.end();
-
- // Draw child media in native size
- ctx.renderer.pushScissor({ x: 0, y: 0, width: srcWidth, height: srcHeight });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
+ const { view: tmpView } = await drawChildToTexture(args, childMedia, {
+ width: srcWidth,
+ height: srcHeight,
+ props: {
...props,
virtualMedia: childMedia,
containerWidth: srcWidth,
containerHeight: srcHeight,
},
- tmpView,
- tmpTex,
- srcWidth,
- srcHeight,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// Get compiled shader pipeline
const { scalerPipeline: pipeline, scalerUniformLayout: uLayout } =
@@ -383,20 +316,7 @@ export const ResizerScalerWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
// Render into targetView
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: targetView,
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, targetView, pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [
@@ -405,20 +325,8 @@ export const ResizerScalerWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 2, resource: tmpView },
],
}),
- );
-
- renderPass.draw(4);
- renderPass.end();
+ ]);
// Create and return load render pass to continue downstream node executions
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- args.pass = finalPass;
+ beginTargetPass(args);
};
diff --git a/nodes/node-rotate/src/renderers/webgpu-renderer.ts b/nodes/node-rotate/src/renderers/webgpu-renderer.ts
index 16b07315..efa2457b 100644
--- a/nodes/node-rotate/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-rotate/src/renderers/webgpu-renderer.ts
@@ -1,6 +1,14 @@
/// <reference types="webgpu" />
import type { VirtualMediaData } from "@framefields/core";
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
import { probeVideoSize } from "@framefields/webgpu-renderers";
import { rotatedSourceSize } from "../shared/config.js";
@@ -32,27 +40,7 @@ struct RotateUniforms {
@group(0) @binding(0) var<uniform> u : RotateUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let p = (in.uv - vec2<f32>(0.5)) * u.outSize;
@@ -84,45 +72,16 @@ function getRotateResources(
const cached = deviceResourceCache.get(device);
if (cached) return cached;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, ["texture", "sampler"]);
const shaderModule = device.createShaderModule({ code: WGSL_ROTATE_SHADER });
- const pipeline = device.createRenderPipeline({
- layout: device.createPipelineLayout({
- bindGroupLayouts: [uniformLayout, textureLayout],
- }),
- vertex: { module: shaderModule, entryPoint: "vs" },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- // The pass clears and writes every pixel: no blending needed.
- targets: [{ format: targetFormat }],
- },
- primitive: { topology: "triangle-strip" },
+ const pipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: [uniformLayout, textureLayout],
+ format: targetFormat,
});
const res: DeviceRotateResources = { uniformLayout, textureLayout, pipeline };
@@ -182,12 +141,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as RotateOp | undefined;
@@ -209,44 +166,16 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sourceHeight = intrinsic?.height ?? props.containerHeight ?? height;
// 1. Render the child upright into an offscreen texture at its own size
- const childTex = ctx.renderer.getTemporaryTexture(sourceWidth, sourceHeight, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const childView = childTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- childView,
- { r: 0, g: 0, b: 0, a: 0 },
- sourceWidth,
- sourceHeight,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({
- x: 0,
- y: 0,
+ const { texture: childTex } = await drawChildToTexture(args, childMedia, {
width: sourceWidth,
height: sourceHeight,
- });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
+ props: {
...props,
containerWidth: sourceWidth,
containerHeight: sourceHeight,
virtualMedia: childMedia,
},
- childView,
- childTex,
- sourceWidth,
- sourceHeight,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ });
// 2. Rotate it into the output
const angle = resolveNumber(op.angle, 90, compositionFrame, fps);
@@ -303,37 +232,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(0, uBindGroup);
- renderPass.setBindGroup(1, tBindGroup);
- renderPass.draw(4);
- renderPass.end();
-
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
+ uBindGroup,
+ tBindGroup,
+ ]);
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1.0);
};
diff --git a/nodes/node-selective-color/src/renderers/webgpu-renderer.ts b/nodes/node-selective-color/src/renderers/webgpu-renderer.ts
index 47ba70d3..cc74e439 100644
--- a/nodes/node-selective-color/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-selective-color/src/renderers/webgpu-renderer.ts
@@ -1,5 +1,13 @@
/// <reference types="webgpu" />
import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+} from "@framefields/node-sdk/gpu";
import type {
ColorAdjustment,
SelectiveColorOperation,
@@ -36,27 +44,7 @@ struct SelectiveColorUniforms {
@group(0) @binding(0) var<uniform> u : SelectiveColorUniforms;
@group(1) @binding(0) var tex : texture_2d<f32>;
@group(1) @binding(1) var samp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn calcAdjustment(cmy: vec3<f32>, adj: vec4<f32>, weight: f32, isAbsolute: bool) -> vec3<f32> {
if (weight <= 0.00001) {
@@ -134,48 +122,23 @@ function getSelectiveColorResources(
): DeviceSelectiveColorResources {
let res = deviceResourceCache.get(device);
if (!res) {
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ ]);
const shaderModule = device.createShaderModule({
label: "selective-color.wgsl",
code: WGSL_SELECTIVE_COLOR_SHADER,
});
- const pipeline = device.createRenderPipeline({
+ const pipeline = createFullscreenPipeline(device, {
label: "SelectiveColorPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [uniformLayout, textureLayout],
- }),
- vertex: { module: shaderModule, entryPoint: "vs" },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: shaderModule,
+ layouts: [uniformLayout, textureLayout],
+ format,
});
res = { pipeline, uniformLayout, textureLayout };
@@ -204,12 +167,10 @@ export const SelectiveColorWebGPURenderer: WebGPUNodeRenderer = async (
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as unknown as
@@ -224,37 +185,7 @@ export const SelectiveColorWebGPURenderer: WebGPUNodeRenderer = async (
const width = targetWidth;
const height = targetHeight;
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- {
- ...props,
- virtualMedia: childMedia,
- },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// Prepare Uniforms
packAdjustment(uniformBufferData, 0, op.reds);
@@ -284,52 +215,18 @@ export const SelectiveColorWebGPURenderer: WebGPUNodeRenderer = async (
...(props.excludeTextures || []),
]);
- const renderPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- renderPass.setPipeline(pipeline);
- renderPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uniformLayout,
entries: [{ binding: 0, resource: { buffer } }],
}),
- );
- renderPass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
textureLayout,
tmpTex,
sampler,
),
- );
- renderPass.draw(4);
- renderPass.end();
-
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: props.opacity ?? 1 },
- );
+ ]);
- (args as unknown as { pass: GPURenderPassEncoder }).pass = finalPass;
+ drawTextureToTarget(args, outTex, props.opacity ?? 1);
};
diff --git a/nodes/node-shadows-highlights/src/index.ts b/nodes/node-shadows-highlights/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-shadows-highlights/src/index.ts
+++ b/nodes/node-shadows-highlights/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-shadows-highlights/src/renderers/webgpu-renderer.ts b/nodes/node-shadows-highlights/src/renderers/webgpu-renderer.ts
index 0532d0a4..d42a208b 100644
--- a/nodes/node-shadows-highlights/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-shadows-highlights/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,20 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ resolveHandleParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { MAX_AMOUNT, MAX_RADIUS, MIN_RADIUS } from "../shared/config.js";
interface ShadowsHighlightsOp {
@@ -81,27 +94,7 @@ struct ShadowsHighlightsUniforms {
@group(2) @binding(6) var colorCorrectionSigTex : texture_2d<f32>;
@group(2) @binding(7) var midtoneContrastSigTex : texture_2d<f32>;
@group(2) @binding(8) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
fn get_luminance(c: vec3<f32>) -> f32 {
return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
@@ -324,85 +317,15 @@ function getShadowsHighlightsResources(
const cached = deviceResourceCache.get(device);
if (cached) return cached;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ "texture",
+ ]);
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 7,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 8,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalTextureLayout = texturesLayout(device, 8);
const shaderModule = device.createShaderModule({
code: WGSL_SHADOWS_HIGHLIGHTS_UNIFORMS,
@@ -412,20 +335,10 @@ function getShadowsHighlightsResources(
bindGroupLayouts: [uniformLayout, textureLayout, signalTextureLayout],
});
- const pipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: {
- module: shaderModule,
- entryPoint: "vs",
- },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- targets: [{ format: targetFormat }],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ const pipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: pipelineLayout,
+ format: targetFormat,
});
const res: DeviceShadowsHighlightsResources = {
@@ -443,12 +356,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as ShadowsHighlightsOp | undefined;
@@ -475,38 +386,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
fallbackVal: number,
clampMin = 0,
clampMax = 1000,
- ): {
- val: number;
- hasStaticSig: boolean;
- sd: ShadowsHighlightsSignalData | null;
- } => {
- if (handleId && op.inputs?.[handleId]?.connectionValid) {
- const outputItem = op.inputs[handleId].outputItem;
- if (outputItem?.type === "Number") {
- const numVal =
- typeof outputItem.data === "number"
- ? outputItem.data
- : ((outputItem.data as { value?: number })?.value ?? fallbackVal);
- return {
- val: Math.max(clampMin, Math.min(clampMax, numVal)),
- hasStaticSig: false,
- sd: null,
- };
- }
- if (outputItem?.type === "Signal" && outputItem.data) {
- return {
- val: fallbackVal,
- hasStaticSig: true,
- sd: outputItem.data as ShadowsHighlightsSignalData,
- };
- }
- }
- return {
- val: Math.max(clampMin, Math.min(clampMax, fallbackVal)),
- hasStaticSig: false,
- sd: null,
- };
- };
+ ) =>
+ resolveHandleParam<ShadowsHighlightsSignalData>(
+ op.inputs,
+ handleId,
+ fallbackVal,
+ clampMin,
+ clampMax,
+ );
const shadowAmountRes = resolveField(
op.shadowAmountHandleId,
@@ -560,34 +447,7 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
);
// 1. Render child media into temporary input texture
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// Check if this is a complete no-op (zero shadow/highlight recovery and zero midtone contrast)
const isNoop =
@@ -599,21 +459,7 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
!midtoneContrastRes.hasStaticSig;
if (isNoop) {
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
- ctx.renderer.drawTexture(
- finalPass,
- tmpTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
- args.pass = finalPass;
+ drawTextureToTarget(args, tmpTex, op.opacity ?? 1.0);
return;
}
@@ -627,14 +473,17 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -642,24 +491,7 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
sd: ShadowsHighlightsSignalData | null;
},
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const shadowAmountView = getSignalView(shadowAmountRes, "shadow_amount_sig");
const shadowTonalWidthView = getSignalView(
@@ -763,28 +595,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
shadowsHighlightsUniformData[10] = 0.0; // isSecondPass = false
const hBuffer = ctx.renderer.getTemporaryBuffer(shadowsHighlightsUniformData);
- const hPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex1.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- hPass.setPipeline(pipeline);
- hPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex1.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: hBuffer } }],
}),
- );
- hPass.setBindGroup(1, createBindGroup(tmpTex, tmpTex));
- hPass.setBindGroup(2, sigBindGroup);
- hPass.draw(4);
- hPass.end();
+ createBindGroup(tmpTex, tmpTex),
+ sigBindGroup,
+ ]);
// Pass 2: Vertical Gaussian convolution + Dynamic Range Recovery
shadowsHighlightsUniformData[0] = 0.0;
@@ -792,45 +610,15 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
shadowsHighlightsUniformData[10] = 1.0; // isSecondPass = true
const vBuffer = ctx.renderer.getTemporaryBuffer(shadowsHighlightsUniformData);
- const vPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex2.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- vPass.setPipeline(pipeline);
- vPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex2.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: vBuffer } }],
}),
- );
- vPass.setBindGroup(1, createBindGroup(outTex1, tmpTex));
- vPass.setBindGroup(2, sigBindGroup);
- vPass.draw(4);
- vPass.end();
+ createBindGroup(outTex1, tmpTex),
+ sigBindGroup,
+ ]);
// 3. Final Pass: Draw result back to targetView using standard drawTexture
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex2,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex2, op.opacity ?? 1.0);
};
diff --git a/nodes/node-shape-generator/src/index.ts b/nodes/node-shape-generator/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-shape-generator/src/index.ts
+++ b/nodes/node-shape-generator/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-shape-generator/src/shared/config.ts b/nodes/node-shape-generator/src/shared/config.ts
index 729f2c9b..1ed91fd0 100644
--- a/nodes/node-shape-generator/src/shared/config.ts
+++ b/nodes/node-shape-generator/src/shared/config.ts
@@ -3,6 +3,7 @@ import {
configBuilder,
createOutputItemSchema,
MultiOutputGenericSchema,
+ OperationInputsSchema,
VirtualMediaDataSchema,
} from "@framefields/node-sdk";
import { z } from "zod";
@@ -208,21 +209,7 @@ export const ShapeGeneratorOperationSchema =
svgContent: z.string().optional(),
svgDataUrl: z.string().optional(),
metadata: z.record(z.string(), z.unknown()).optional(),
- inputs: z
- .record(
- z.string(),
- z.object({
- connectionValid: z.boolean(),
- outputItem: z
- .object({
- type: z.string(),
- data: z.unknown(),
- })
- .nullable()
- .optional(),
- }),
- )
- .optional(),
+ inputs: OperationInputsSchema.optional(),
});
export type ShapeGeneratorOperation = z.infer<
typeof ShapeGeneratorOperationSchema
diff --git a/nodes/node-tile-offset/src/index.ts b/nodes/node-tile-offset/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-tile-offset/src/index.ts
+++ b/nodes/node-tile-offset/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-tile-offset/src/renderers/webgpu-renderer.ts b/nodes/node-tile-offset/src/renderers/webgpu-renderer.ts
index 572c10ff..73dedfae 100644
--- a/nodes/node-tile-offset/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-tile-offset/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,21 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ unwrapFrameParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
interface TileOffsetOp {
op: "TileOffset";
@@ -46,27 +60,7 @@ struct TileOffsetUniforms {
@group(2) @binding(0) var offsetXSigTex : texture_2d<f32>;
@group(2) @binding(1) var offsetYSigTex : texture_2d<f32>;
@group(2) @binding(2) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
var offX = u.offsetX;
@@ -109,74 +103,14 @@ struct VSOut {
}
`;
-interface DeviceTileOffsetResources {
- uniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceTileOffsetResources>();
const uniformData = new Float32Array(8);
-function getDeviceLayouts(device: GPUDevice): DeviceTileOffsetResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- singleTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: texturesLayout(device, 2),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getTileOffsetResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -189,22 +123,15 @@ function getTileOffsetResources(device: GPUDevice, format: GPUTextureFormat) {
code: TILE_OFFSET_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `TileOffsetPipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.singleTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module, entryPoint: "vs" },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.singleTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -222,12 +149,10 @@ export const TileOffsetWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -273,28 +198,11 @@ export const TileOffsetWebGPURenderer: WebGPUNodeRenderer = async (args) => {
if (input?.connectionValid && input.outputItem?.type === "Number") {
val = Number(input.outputItem.data ?? defaultValue);
} else {
- let rawVal = op[configKey] as unknown;
- if (rawVal !== undefined && rawVal !== null) {
- if (typeof rawVal === "object") {
- if (
- "get" in (rawVal as Record<string, unknown>) &&
- typeof (rawVal as Record<string, unknown>).get === "function"
- ) {
- rawVal = (
- rawVal as {
- get: (ctx: { frame: number; fps: number }) => unknown;
- }
- ).get({ frame: compositionFrame, fps });
- } else if ("value" in (rawVal as Record<string, unknown>)) {
- rawVal = (rawVal as { value: unknown }).value;
- }
- } else if (typeof rawVal === "function") {
- rawVal = (rawVal as (f: number, fps: number) => unknown)(
- compositionFrame,
- fps,
- );
- }
- }
+ const rawVal = unwrapFrameParam(
+ op[configKey],
+ compositionFrame,
+ fps,
+ ).value;
const numVal = Number(rawVal ?? defaultValue);
val = isNaN(numVal) ? defaultValue : numVal;
}
@@ -321,34 +229,7 @@ export const TileOffsetWebGPURenderer: WebGPUNodeRenderer = async (args) => {
}
// 2. Draw child media to offscreen texture
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// End active pass from drawing child
args.pass.end();
@@ -364,14 +245,17 @@ export const TileOffsetWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
// Fetch dynamic signal texture views on-demand
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -379,24 +263,7 @@ export const TileOffsetWebGPURenderer: WebGPUNodeRenderer = async (args) => {
sd: TileOffsetSignalData | null;
},
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const offsetXView = getSignalView(offsetXRes, "offsetX_sig");
const offsetYView = getSignalView(offsetYRes, "offsetY_sig");
@@ -421,36 +288,17 @@ export const TileOffsetWebGPURenderer: WebGPUNodeRenderer = async (args) => {
]);
// 6. Run tile offset pass
- const tilePass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- tilePass.setPipeline(pipeline);
- tilePass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- tilePass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout,
tmpTex,
sampler,
),
- );
- tilePass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -459,28 +307,10 @@ export const TileOffsetWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 2, resource: sampler },
],
}),
- );
- tilePass.draw(4);
- tilePass.end();
+ ]);
// 7. Output result back to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1.0);
};
export const WebGPURenderer = TileOffsetWebGPURenderer;
diff --git a/nodes/node-tile-offset/src/shared/config.ts b/nodes/node-tile-offset/src/shared/config.ts
index 4ad8a13c..7acf89c7 100644
--- a/nodes/node-tile-offset/src/shared/config.ts
+++ b/nodes/node-tile-offset/src/shared/config.ts
@@ -1,6 +1,7 @@
import {
configBuilder,
ImageResultSchema,
+ OperationInputsSchema,
VideoResultSchema,
} from "@framefields/node-sdk";
import { z } from "zod";
@@ -50,21 +51,7 @@ export const TileOffsetOperationSchema = TileOffsetNodeConfigSchema.extend({
metadata: z.record(z.string(), z.unknown()).optional(),
offsetXHandleId: z.string().nullable().optional(),
offsetYHandleId: z.string().nullable().optional(),
- inputs: z
- .record(
- z.string(),
- z.object({
- connectionValid: z.boolean(),
- outputItem: z
- .object({
- type: z.string(),
- data: z.unknown(),
- })
- .nullable()
- .optional(),
- }),
- )
- .optional(),
+ inputs: OperationInputsSchema.optional(),
});
export type TileOffsetOperation = z.infer<typeof TileOffsetOperationSchema>;
diff --git a/nodes/node-unsharp-mask/src/index.ts b/nodes/node-unsharp-mask/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-unsharp-mask/src/index.ts
+++ b/nodes/node-unsharp-mask/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-unsharp-mask/src/renderers/webgpu-renderer.ts b/nodes/node-unsharp-mask/src/renderers/webgpu-renderer.ts
index 4b171db1..b436c6fc 100644
--- a/nodes/node-unsharp-mask/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-unsharp-mask/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,20 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ resolveHandleParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
import { MAX_AMOUNT, MAX_RADIUS, MAX_THRESHOLD } from "../shared/config.js";
interface UnsharpMaskOp {
@@ -54,27 +67,7 @@ struct UnsharpMaskUniforms {
@group(2) @binding(1) var amountSigTex : texture_2d<f32>;
@group(2) @binding(2) var thresholdSigTex : texture_2d<f32>;
@group(2) @binding(3) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let dimensions = vec2<f32>(textureDimensions(tex));
@@ -165,60 +158,15 @@ function getUnsharpMaskResources(
const cached = deviceResourceCache.get(device);
if (cached) return cached;
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ const uniformLayout = fragmentBindGroupLayout(device, ["uniform"]);
- const textureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- ],
- });
+ const textureLayout = fragmentBindGroupLayout(device, [
+ "texture",
+ "sampler",
+ "texture",
+ ]);
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
+ const signalTextureLayout = texturesLayout(device, 3);
const shaderModule = device.createShaderModule({
code: WGSL_UNSHARPMASK_UNIFORMS,
@@ -228,20 +176,10 @@ function getUnsharpMaskResources(
bindGroupLayouts: [uniformLayout, textureLayout, signalTextureLayout],
});
- const pipeline = device.createRenderPipeline({
- layout: pipelineLayout,
- vertex: {
- module: shaderModule,
- entryPoint: "vs",
- },
- fragment: {
- module: shaderModule,
- entryPoint: "fs",
- targets: [{ format: targetFormat }],
- },
- primitive: {
- topology: "triangle-strip",
- },
+ const pipeline = createFullscreenPipeline(device, {
+ module: shaderModule,
+ layouts: pipelineLayout,
+ format: targetFormat,
});
const res: DeviceUnsharpMaskResources = {
@@ -259,12 +197,10 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const op = props.virtualMedia?.operation as UnsharpMaskOp | undefined;
@@ -287,38 +223,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
fallbackVal: number,
clampMin = 0,
clampMax = 1000,
- ): {
- val: number;
- hasStaticSig: boolean;
- sd: UnsharpMaskSignalData | null;
- } => {
- if (handleId && op.inputs?.[handleId]?.connectionValid) {
- const outputItem = op.inputs[handleId].outputItem;
- if (outputItem?.type === "Number") {
- const numVal =
- typeof outputItem.data === "number"
- ? outputItem.data
- : ((outputItem.data as { value?: number })?.value ?? fallbackVal);
- return {
- val: Math.max(clampMin, Math.min(clampMax, numVal)),
- hasStaticSig: false,
- sd: null,
- };
- }
- if (outputItem?.type === "Signal" && outputItem.data) {
- return {
- val: fallbackVal,
- hasStaticSig: true,
- sd: outputItem.data as UnsharpMaskSignalData,
- };
- }
- }
- return {
- val: Math.max(clampMin, Math.min(clampMax, fallbackVal)),
- hasStaticSig: false,
- sd: null,
- };
- };
+ ) =>
+ resolveHandleParam<UnsharpMaskSignalData>(
+ op.inputs,
+ handleId,
+ fallbackVal,
+ clampMin,
+ clampMax,
+ );
const amountRes = resolveField(
op.amountHandleId,
@@ -340,34 +252,7 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
);
// 1. Render child media into temporary input texture
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// 2. Fetch static resources and compile pipeline once
const {
@@ -379,14 +264,17 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -394,24 +282,7 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
sd: UnsharpMaskSignalData | null;
},
suffix: string,
- ) => {
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const radiusView = getSignalView(radiusRes, "radius_sig");
const amountView = getSignalView(amountRes, "amount_sig");
@@ -470,28 +341,14 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
unsharpMaskUniformData[8] = 0.0; // isSecondPass = false
const hBuffer = ctx.renderer.getTemporaryBuffer(unsharpMaskUniformData);
- const hPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex1.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- hPass.setPipeline(pipeline);
- hPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex1.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: hBuffer } }],
}),
- );
- hPass.setBindGroup(1, createUnsharpMaskBindGroup(tmpTex, tmpTex));
- hPass.setBindGroup(2, sigBindGroup);
- hPass.draw(4);
- hPass.end();
+ createUnsharpMaskBindGroup(tmpTex, tmpTex),
+ sigBindGroup,
+ ]);
// Pass 2: Vertical Gaussian convolution + Precision Unsharp Mask against original tmpTex
unsharpMaskUniformData[0] = 0.0;
@@ -499,45 +356,15 @@ export const WebGPURenderer: WebGPUNodeRenderer = async (args) => {
unsharpMaskUniformData[8] = 1.0; // isSecondPass = true
const vBuffer = ctx.renderer.getTemporaryBuffer(unsharpMaskUniformData);
- const vPass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex2.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
- vPass.setPipeline(pipeline);
- vPass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex2.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: vBuffer } }],
}),
- );
- vPass.setBindGroup(1, createUnsharpMaskBindGroup(outTex1, tmpTex));
- vPass.setBindGroup(2, sigBindGroup);
- vPass.draw(4);
- vPass.end();
+ createUnsharpMaskBindGroup(outTex1, tmpTex),
+ sigBindGroup,
+ ]);
// 3. Final Pass: Draw result back to targetView using standard drawTexture for blending/opacity
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex2,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex2, op.opacity ?? 1.0);
};
diff --git a/nodes/node-vignette/src/index.ts b/nodes/node-vignette/src/index.ts
index 34f8d698..944d702e 100644
--- a/nodes/node-vignette/src/index.ts
+++ b/nodes/node-vignette/src/index.ts
@@ -1,2 +1,2 @@
-export * from "./shared/index.js";
export * from "./renderers/index.js";
+export * from "./shared/index.js";
diff --git a/nodes/node-vignette/src/renderers/webgpu-renderer.ts b/nodes/node-vignette/src/renderers/webgpu-renderer.ts
index 81813917..7f5265d9 100644
--- a/nodes/node-vignette/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-vignette/src/renderers/webgpu-renderer.ts
@@ -1,7 +1,21 @@
/// <reference types="webgpu" />
import type { SignalData } from "@framefields/core";
-import type { WebGPUNodeRenderer } from "@framefields/node-sdk";
-import { signalRegistry } from "@framefields/webgpu-renderers";
+import {
+ unwrapFrameParam,
+ type WebGPUNodeRenderer,
+} from "@framefields/node-sdk";
+import {
+ createFullscreenPipeline,
+ drawChildToTexture,
+ drawFullscreenPass,
+ drawTextureToTarget,
+ FULLSCREEN_QUAD_WGSL,
+ fragmentBindGroupLayout,
+ perDevice,
+ signalTextureView,
+ signalTiming,
+ texturesLayout,
+} from "@framefields/node-sdk/gpu";
interface VignetteOp {
op: "Vignette";
@@ -61,27 +75,7 @@ struct VignetteUniforms {
@group(2) @binding(4) var centerXSigTex : texture_2d<f32>;
@group(2) @binding(5) var centerYSigTex : texture_2d<f32>;
@group(2) @binding(6) var signalSamp : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4<f32>,
- @location(0) uv : vec2<f32>,
-};
-
-@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
- var pos = array<vec2<f32>, 4>(
- vec2<f32>(-1.0, 1.0),
- vec2<f32>(1.0, 1.0),
- vec2<f32>(-1.0, -1.0),
- vec2<f32>(1.0, -1.0)
- );
- var uv = array<vec2<f32>, 4>(
- vec2<f32>(0.0, 0.0),
- vec2<f32>(1.0, 0.0),
- vec2<f32>(0.0, 1.0),
- vec2<f32>(1.0, 1.0)
- );
- return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
-}
+${FULLSCREEN_QUAD_WGSL}
@fragment fn fs(in : VSOut) -> @location(0) vec4<f32> {
let color = textureSampleLevel(tex, samp, in.uv, 0.0);
@@ -150,94 +144,14 @@ struct VSOut {
}
`;
-interface DeviceVignetteResources {
- uniformLayout: GPUBindGroupLayout;
- singleTextureLayout: GPUBindGroupLayout;
- signalTextureLayout: GPUBindGroupLayout;
- pipelineCache: Map<string, GPURenderPipeline>;
-}
-
-const deviceResourceCache = new WeakMap<GPUDevice, DeviceVignetteResources>();
const vignetteUniformData = new Float32Array(12);
-function getDeviceLayouts(device: GPUDevice): DeviceVignetteResources {
- let res = deviceResourceCache.get(device);
- if (res) return res;
-
- const uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
-
- const singleTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- const signalTextureLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 1,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 2,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 3,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 4,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 5,
- visibility: GPUShaderStage.FRAGMENT,
- texture: { sampleType: "float" },
- },
- {
- binding: 6,
- visibility: GPUShaderStage.FRAGMENT,
- sampler: { type: "filtering" },
- },
- ],
- });
-
- res = {
- uniformLayout,
- singleTextureLayout,
- signalTextureLayout,
- pipelineCache: new Map(),
- };
- deviceResourceCache.set(device, res);
- return res;
-}
+const getDeviceLayouts = perDevice((device) => ({
+ uniformLayout: fragmentBindGroupLayout(device, ["uniform"]),
+ singleTextureLayout: fragmentBindGroupLayout(device, ["texture", "sampler"]),
+ signalTextureLayout: texturesLayout(device, 6),
+ pipelineCache: new Map<string, GPURenderPipeline>(),
+}));
function getVignetteResources(device: GPUDevice, format: GPUTextureFormat) {
const layouts = getDeviceLayouts(device);
@@ -250,22 +164,15 @@ function getVignetteResources(device: GPUDevice, format: GPUTextureFormat) {
code: VIGNETTE_SHADER,
});
- pipeline = device.createRenderPipeline({
+ pipeline = createFullscreenPipeline(device, {
label: `VignettePipeline_${format}`,
- layout: device.createPipelineLayout({
- bindGroupLayouts: [
- layouts.uniformLayout,
- layouts.singleTextureLayout,
- layouts.signalTextureLayout,
- ],
- }),
- vertex: { module: vignetteModule, entryPoint: "vs" },
- fragment: {
- module: vignetteModule,
- entryPoint: "fs",
- targets: [{ format }],
- },
- primitive: { topology: "triangle-strip" },
+ module: vignetteModule,
+ layouts: [
+ layouts.uniformLayout,
+ layouts.singleTextureLayout,
+ layouts.signalTextureLayout,
+ ],
+ format,
});
layouts.pipelineCache.set(cacheKey, pipeline);
}
@@ -283,12 +190,10 @@ export const VignetteWebGPURenderer: WebGPUNodeRenderer = async (args) => {
ctx,
encoder,
pass,
- targetView,
targetTexture,
targetWidth,
targetHeight,
props,
- drawChild,
} = args;
const frame = props.frame ?? 0;
@@ -347,36 +252,16 @@ export const VignetteWebGPURenderer: WebGPUNodeRenderer = async (args) => {
Math.min(maxVal, Number(input.outputItem.data ?? defaultValue)),
);
} else {
- let rawVal = op[configKey] as unknown;
- if (rawVal !== undefined && rawVal !== null) {
- if (typeof rawVal === "object") {
- if (
- "get" in (rawVal as Record<string, unknown>) &&
- typeof (rawVal as Record<string, unknown>).get === "function"
- ) {
- rawVal = (
- rawVal as {
- get: (ctx: { frame: number; fps: number }) => unknown;
- }
- ).get({ frame: compositionFrame, fps });
- } else if ("value" in (rawVal as Record<string, unknown>)) {
- const sig = rawVal as {
- value: unknown;
- gpuBinding?: { textureView?: GPUTextureView };
- };
- if (sig.gpuBinding?.textureView) {
- gpuTextureView = sig.gpuBinding.textureView;
- hasStaticSig = true;
- }
- rawVal = sig.value;
- }
- } else if (typeof rawVal === "function") {
- rawVal = (rawVal as (f: number, fps: number) => unknown)(
- compositionFrame,
- fps,
- );
- }
+ const unwrapped = unwrapFrameParam(
+ op[configKey],
+ compositionFrame,
+ fps,
+ );
+ if (unwrapped.gpuTextureView) {
+ gpuTextureView = unwrapped.gpuTextureView;
+ hasStaticSig = true;
}
+ const rawVal = unwrapped.value;
const numVal = Number(rawVal ?? defaultValue);
val = Math.max(
minVal,
@@ -399,34 +284,7 @@ export const VignetteWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const centerYRes = resolveBindable("centerY", 0.5, 0.0, 1.0);
// 2. Draw child media
- const tmpTex = ctx.renderer.getTemporaryTexture(width, height, [
- ...(props.excludeTextures || []),
- targetTexture,
- ]);
- const tmpView = tmpTex.createView();
-
- const childPass = ctx.renderer.beginFrame(
- encoder,
- tmpView,
- { r: 0, g: 0, b: 0, a: 0 },
- width,
- height,
- "clear",
- );
- childPass.end();
-
- ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
- ctx.renderer.pushIdentity();
- await drawChild(
- childMedia,
- { ...props, virtualMedia: childMedia },
- tmpView,
- tmpTex,
- width,
- height,
- );
- ctx.renderer.popTransform();
- ctx.renderer.popScissor();
+ const { texture: tmpTex } = await drawChildToTexture(args, childMedia);
// End any active pass from drawing child
args.pass.end();
@@ -442,14 +300,17 @@ export const VignetteWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const sampler = ctx.renderer.samplerCache.getSampler(ctx.device);
// Fetch dynamic signal texture views on-demand
- const elapsedSeconds =
- props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps;
- const durationSeconds = props.virtualMedia?.metadata?.durationMs
- ? props.virtualMedia.metadata.durationMs / 1000
- : props.durationMs !== undefined
- ? props.durationMs / 1000
- : 0;
+ const { elapsedSeconds, durationSeconds } = signalTiming(props, frame, fps);
+ const signalContext = {
+ args,
+ width,
+ height,
+ frame,
+ fps,
+ elapsedSeconds,
+ durationSeconds,
+ };
const getSignalView = (
res: {
val: number;
@@ -458,27 +319,7 @@ export const VignetteWebGPURenderer: WebGPUNodeRenderer = async (args) => {
gpuTextureView?: GPUTextureView;
},
suffix: string,
- ) => {
- if (res.gpuTextureView) {
- return res.gpuTextureView;
- }
- if (res.hasStaticSig && res.sd) {
- return signalRegistry.getOrCreate2DTextureView(
- ctx.device,
- encoder,
- res.sd.nodeId ?? `${nodeId}_${suffix}`,
- elapsedSeconds,
- durationSeconds,
- res.sd,
- width,
- height,
- props.renderId,
- frame,
- fps,
- );
- }
- return signalRegistry.getDummy1x1TextureView(ctx.device);
- };
+ ) => signalTextureView(signalContext, res, `${nodeId}_${suffix}`);
const strengthView = getSignalView(strengthRes, "strength_sig");
const radiusView = getSignalView(radiusRes, "radius_sig");
@@ -511,36 +352,17 @@ export const VignetteWebGPURenderer: WebGPUNodeRenderer = async (args) => {
]);
// 6. Run vignette pass
- const vignettePass = encoder.beginRenderPass({
- colorAttachments: [
- {
- view: outTex.createView(),
- loadOp: "clear",
- storeOp: "store",
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
- },
- ],
- });
-
- vignettePass.setPipeline(pipeline);
- vignettePass.setBindGroup(
- 0,
+ drawFullscreenPass(encoder, outTex.createView(), pipeline, [
ctx.device.createBindGroup({
layout: uLayout,
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
}),
- );
- vignettePass.setBindGroup(
- 1,
ctx.renderer.bindGroupCache.getBindGroup(
ctx.device,
tLayout,
tmpTex,
sampler,
),
- );
- vignettePass.setBindGroup(
- 2,
ctx.device.createBindGroup({
layout: sigLayout,
entries: [
@@ -553,26 +375,8 @@ export const VignetteWebGPURenderer: WebGPUNodeRenderer = async (args) => {
{ binding: 6, resource: sampler },
],
}),
- );
- vignettePass.draw(4);
- vignettePass.end();
+ ]);
// 7. Output result back to targetView
- const finalPass = ctx.renderer.beginFrame(
- encoder,
- targetView,
- { r: 0, g: 0, b: 0, a: 0 },
- targetWidth,
- targetHeight,
- "load",
- );
-
- ctx.renderer.drawTexture(
- finalPass,
- outTex,
- { x: 0, y: 0, width: targetWidth, height: targetHeight },
- { opacity: op.opacity ?? 1.0 },
- );
-
- args.pass = finalPass;
+ drawTextureToTarget(args, outTex, op.opacity ?? 1.0);
};
diff --git a/packages/compositions/src/compositions/canvas-composition.ts b/packages/compositions/src/compositions/canvas-composition.ts
index 4e5b7037..5355119d 100644
--- a/packages/compositions/src/compositions/canvas-composition.ts
+++ b/packages/compositions/src/compositions/canvas-composition.ts
@@ -85,6 +85,70 @@ function resolveSignalOperation(
return { ...virtualMedia, operation: resolved } as VirtualMediaData;
}
+/** The paragraph props a text operation maps to, shared by both text paths. */
+function paragraphPropsFromTextOp(
+ // biome-ignore lint/suspicious/noExplicitAny: loosely typed operation payload
+ opText: any,
+ props: NodeRenderProps,
+ timing: {
+ containerWidth: number;
+ containerHeight: number;
+ frame: number;
+ fps: number;
+ durationMs: number | undefined;
+ elapsedMs: number | undefined;
+ },
+): Parameters<typeof drawParagraphNode>[2] {
+ return {
+ text: opText.text || "",
+ dstRect: {
+ x: Number(opText.x ?? 0),
+ y: Number(opText.y ?? 0),
+ width: Number(opText.width ?? timing.containerWidth),
+ height: Number(opText.height ?? timing.containerHeight),
+ },
+ fontFamily: opText.fontFamily,
+ fontSize: Number(opText.fontSize ?? 48),
+ color: opText.fill ?? opText.color ?? "white",
+ width: opText.width ? Number(opText.width) : undefined,
+ height: opText.height ? Number(opText.height) : undefined,
+ lineHeight: Number(opText.lineHeight ?? 1.2),
+ align: opText.align,
+ verticalAlign: opText.verticalAlign,
+ fontWeight: opText.fontWeight,
+ fontStyle: opText.fontStyle,
+ letterSpacing: opText.letterSpacing,
+ shadows: opText.shadows,
+ textBackgroundColor: opText.textBackgroundColor,
+ padding: opText.padding != null ? Number(opText.padding) : undefined,
+ stroke: opText.stroke,
+ strokeWidth:
+ (opText.strokeWidth ?? opText.strokeThickness)
+ ? Number(opText.strokeWidth ?? opText.strokeThickness)
+ : undefined,
+ strokeAlign: opText.strokeAlign,
+ opacity: Number(opText.opacity ?? 1) * Number(props.opacity ?? 1),
+ isHeadless: props.isHeadless,
+ animation: opText.animation ?? opText.textAnimation,
+ animators: opText.animators,
+ pathOptions: opText.pathOptions,
+ typewriter: opText.typewriter,
+ marquee: opText.marquee,
+ signals: props.signals ?? opText.signals,
+ firstMargin:
+ opText.firstMargin != null ? Number(opText.firstMargin) : undefined,
+ lastMargin:
+ opText.lastMargin != null ? Number(opText.lastMargin) : undefined,
+ baselineShift:
+ opText.baselineShift != null ? Number(opText.baselineShift) : undefined,
+ frame: timing.frame,
+ fps: timing.fps,
+ durationMs: timing.durationMs,
+ elapsedMs: timing.elapsedMs,
+ isVideoMode: props.isVideoMode,
+ };
+}
+
export async function drawCompositionTree(
ctx: RenderContextValue,
encoder: GPUCommandEncoder,
@@ -461,65 +525,22 @@ export async function drawCompositionTree(
} else if (mediaType === "Text") {
const opText = { ...op } as any;
drawParagraphNode(ctx, pass, {
- text: opText.text || "",
- dstRect: {
- x: Number(opText.x ?? 0),
- y: Number(opText.y ?? 0),
- width: Number(opText.width ?? containerWidth),
- height: Number(opText.height ?? containerHeight),
- },
- fontFamily: opText.fontFamily,
- fontSize: Number(opText.fontSize ?? 48),
- color: opText.fill ?? opText.color ?? "white",
- width: opText.width ? Number(opText.width) : undefined,
- height: opText.height ? Number(opText.height) : undefined,
- lineHeight: Number(opText.lineHeight ?? 1.2),
- align: opText.align,
- verticalAlign: opText.verticalAlign,
- fontWeight: opText.fontWeight,
- fontStyle: opText.fontStyle,
- letterSpacing: opText.letterSpacing,
- shadows: opText.shadows,
- textBackgroundColor: opText.textBackgroundColor,
+ ...paragraphPropsFromTextOp(opText, props, {
+ containerWidth,
+ containerHeight,
+ frame: localFrame,
+ fps,
+ durationMs: resolvedDurationMs,
+ elapsedMs: resolvedElapsedMs,
+ }),
borderRadius:
opText.borderRadius != null
? Number(opText.borderRadius)
: undefined,
- padding:
- opText.padding != null ? Number(opText.padding) : undefined,
- stroke: opText.stroke,
- strokeWidth:
- (opText.strokeWidth ?? opText.strokeThickness)
- ? Number(opText.strokeWidth ?? opText.strokeThickness)
- : undefined,
- strokeAlign: opText.strokeAlign,
- opacity: Number(opText.opacity ?? 1) * Number(props.opacity ?? 1),
- isHeadless: props.isHeadless,
- animation: opText.animation ?? opText.textAnimation,
- animators: opText.animators,
- pathOptions: opText.pathOptions,
- typewriter: opText.typewriter,
- marquee: opText.marquee,
textProgress:
opText.textProgress != null
? Number(opText.textProgress)
: undefined,
- signals: props.signals ?? opText.signals,
- firstMargin:
- opText.firstMargin != null
- ? Number(opText.firstMargin)
- : undefined,
- lastMargin:
- opText.lastMargin != null ? Number(opText.lastMargin) : undefined,
- baselineShift:
- opText.baselineShift != null
- ? Number(opText.baselineShift)
- : undefined,
- frame: localFrame,
- fps,
- durationMs: resolvedDurationMs,
- elapsedMs: resolvedElapsedMs,
- isVideoMode: props.isVideoMode,
});
} else if (mediaType === "Video") {
const finalPlaybackRate = Math.max(
@@ -611,28 +632,16 @@ export async function drawCompositionTree(
const containerWidth = props.containerWidth ?? ctx.surface.width;
const containerHeight = props.containerHeight ?? ctx.surface.height;
drawParagraphNode(ctx, pass, {
+ ...paragraphPropsFromTextOp(opText, props, {
+ containerWidth,
+ containerHeight,
+ frame: localFrame,
+ fps,
+ durationMs: resolvedDurationMs,
+ elapsedMs: resolvedElapsedMs,
+ }),
renderId: props.renderId,
- text: opText.text || "",
- dstRect: {
- x: Number(opText.x ?? 0),
- y: Number(opText.y ?? 0),
- width: Number(opText.width ?? containerWidth),
- height: Number(opText.height ?? containerHeight),
- },
spans: opText.spans,
- fontFamily: opText.fontFamily,
- fontSize: Number(opText.fontSize ?? 48),
- color: opText.fill ?? opText.color ?? "white",
- width: opText.width ? Number(opText.width) : undefined,
- height: opText.height ? Number(opText.height) : undefined,
- lineHeight: Number(opText.lineHeight ?? 1.2),
- align: opText.align,
- verticalAlign: opText.verticalAlign,
- fontWeight: opText.fontWeight,
- fontStyle: opText.fontStyle,
- letterSpacing: opText.letterSpacing,
- shadows: opText.shadows,
- textBackgroundColor: opText.textBackgroundColor,
borderRadius:
(opText.borderRadius ?? opText.strokeRadius) != null
? Number(opText.borderRadius ?? opText.strokeRadius)
@@ -641,34 +650,6 @@ export async function drawCompositionTree(
(opText.strokeRadius ?? opText.borderRadius) != null
? Number(opText.strokeRadius ?? opText.borderRadius)
: undefined,
- padding: opText.padding != null ? Number(opText.padding) : undefined,
- stroke: opText.stroke,
- strokeWidth:
- (opText.strokeWidth ?? opText.strokeThickness)
- ? Number(opText.strokeWidth ?? opText.strokeThickness)
- : undefined,
- strokeAlign: opText.strokeAlign,
- opacity: Number(opText.opacity ?? 1) * Number(props.opacity ?? 1),
- isHeadless: props.isHeadless,
- animation: opText.animation ?? opText.textAnimation,
- animators: opText.animators,
- pathOptions: opText.pathOptions,
- typewriter: opText.typewriter,
- marquee: opText.marquee,
- signals: props.signals ?? opText.signals,
- firstMargin:
- opText.firstMargin != null ? Number(opText.firstMargin) : undefined,
- lastMargin:
- opText.lastMargin != null ? Number(opText.lastMargin) : undefined,
- baselineShift:
- opText.baselineShift != null
- ? Number(opText.baselineShift)
- : undefined,
- frame: localFrame,
- fps,
- durationMs: resolvedDurationMs,
- elapsedMs: resolvedElapsedMs,
- isVideoMode: props.isVideoMode,
});
}
diff --git a/packages/compositions/src/program/path-follower.test.ts b/packages/compositions/src/program/path-follower.test.ts
index 86c1272d..98b61143 100644
--- a/packages/compositions/src/program/path-follower.test.ts
+++ b/packages/compositions/src/program/path-follower.test.ts
@@ -1,4 +1,4 @@
-import { Path3D } from "@framefields/renderers";
+import { Path3D } from "@framefields/webgpu-renderers";
import { describe, expect, it } from "vitest";
import { CameraAnimation, LayerAnimation } from "./animation.js";
diff --git a/packages/compositions/src/program/schema.ts b/packages/compositions/src/program/schema.ts
index 532d5629..5882975d 100644
--- a/packages/compositions/src/program/schema.ts
+++ b/packages/compositions/src/program/schema.ts
@@ -22,7 +22,6 @@ import type {
import { ColorSchema } from "@framefields/node-sdk";
export type { TextSpan, TextSpanMark };
-import { z } from "zod";
import {
type AdvancedTextAnimator,
AdvancedTextAnimatorSchema,
@@ -34,9 +33,10 @@ import {
TextPathOptionsSchema,
type TypewriterAnimator,
TypewriterAnimatorSchema,
-} from "./text-animations/index.js";
+} from "@framefields/webgpu-renderers/text-animations";
+import { z } from "zod";
-export * from "./text-animations/index.js";
+export * from "@framefields/webgpu-renderers/text-animations";
// ── shared leaf primitives ────────────────────────────────────────────
diff --git a/packages/compositions/src/program/text-animations/advanced-builder.ts b/packages/compositions/src/program/text-animations/advanced-builder.ts
deleted file mode 100644
index a4f6b8c0..00000000
--- a/packages/compositions/src/program/text-animations/advanced-builder.ts
+++ /dev/null
@@ -1,312 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/advanced-builder.ts
- * Fluent builder for Advanced Motion Design & After Effects Extended Typography Animators.
- */
-
-import type {
- AnchorGrouping,
- AnchorPreset,
- ExpressionSelector,
- ExtendedGlyphProperties,
- VariableFontAxes,
- VolumetricFormationMode,
-} from "./advanced-types.js";
-
-/**
- * Fluent Builder for Extended Text Animators.
- */
-export class AdvancedTextAnimatorBuilder {
- private readonly name: string;
- private props: ExtendedGlyphProperties = {};
- private selectors: (ExpressionSelector | Record<string, unknown>)[] = [];
-
- constructor(name: string) {
- this.name = name;
- }
-
- static create(name: string): AdvancedTextAnimatorBuilder {
- return new AdvancedTextAnimatorBuilder(name);
- }
-
- /** Directional Skew */
- skew(angleDeg: number, axisDeg = 0): this {
- this.props.skew = angleDeg;
- this.props.skewAxis = axisDeg;
- return this;
- }
-
- /** Anchor Point Grouping and Preset Pivot */
- anchor(
- preset: AnchorPreset,
- grouping: AnchorGrouping = "character",
- offsetX = 0,
- offsetY = 0,
- ): this {
- this.props.anchor = {
- grouping,
- preset,
- anchorX: 0.5,
- anchorY: 0.5,
- offsetX,
- offsetY,
- };
- return this;
- }
-
- /** Custom Anchor Point Coordinates (0 to 1 normalized) */
- anchorNormalized(
- ax: number,
- ay: number,
- grouping: AnchorGrouping = "character",
- ): this {
- this.props.anchor = {
- grouping,
- preset: "custom",
- anchorX: ax,
- anchorY: ay,
- offsetX: 0,
- offsetY: 0,
- };
- return this;
- }
-
- /** 3D Spatial Z-Depth */
- translateZ(zPixels: number): this {
- this.props.translateZ = zPixels;
- return this;
- }
-
- /** 3D Tilts */
- tilt3D(rx = 0, ry = 0, rz = 0): this {
- this.props.rotationX = rx;
- this.props.rotationY = ry;
- this.props.rotationZ = rz;
- return this;
- }
-
- /** 3D Rotation along X axis */
- rotationX(deg: number): this {
- this.props.rotationX = deg;
- return this;
- }
-
- /** 3D Rotation along Y axis */
- rotationY(deg: number): this {
- this.props.rotationY = deg;
- return this;
- }
-
- /** 3D Rotation along Z axis */
- rotationZ(deg: number): this {
- this.props.rotationZ = deg;
- return this;
- }
-
- /** 3D Volumetric Formations */
- volumetricFormation(
- mode: VolumetricFormationMode = "cylinder",
- radius = 300,
- pitch = 120,
- totalAngle = 360,
- perspective = 1200,
- axis: "x" | "y" | "z" = "y",
- ): this {
- this.props.formation = {
- mode,
- radius,
- pitch,
- totalAngle,
- perspective,
- axis,
- };
- return this;
- }
-
- /** Dynamic Line Spacing (Leading) */
- lineSpacing(extraLeadingPixels: number, lineAnchor = 0): this {
- this.props.lineSpacing = extraLeadingPixels;
- this.props.lineAnchor = lineAnchor;
- return this;
- }
-
- /** Normalized Line Anchor (0=top, 0.5=center, 1=bottom) */
- lineAnchor(anchor: number): this {
- this.props.lineAnchor = anchor;
- return this;
- }
-
- /** Inertial Spring Settling Physics */
- spring(stiffness = 180, damping = 12, mass = 1.0, initialVelocity = 0): this {
- this.props.spring = {
- stiffness,
- damping,
- mass,
- initialVelocity,
- overshootMultiplier: 1.0,
- };
- return this;
- }
-
- /** Dynamic Stroke & Wireframe Properties */
- stroke(widthPixels: number, color = "#ffffff", fillOpacity = 1.0): this {
- this.props.stroke = {
- strokeWidth: widthPixels,
- strokeColor: color,
- strokeAlign: "center",
- fillOpacity,
- dashOffset: 0,
- };
- return this;
- }
-
- /** Variable Font Axes */
- variableFont(axes: VariableFontAxes): this {
- this.props.variableAxes = axes;
- return this;
- }
-
- /** Chromatic Aberration / Cyberpunk Glitch */
- chromaticGlitch(
- redShiftX: number,
- redShiftY = 0,
- blueShiftX = -redShiftX,
- blueShiftY = 0,
- ): this {
- this.props.chromaticGlitch = {
- redShiftX,
- redShiftY,
- blueShiftX,
- blueShiftY,
- sliceProbability: 0,
- sliceAmplitude: 0,
- };
- return this;
- }
-
- /** Multi-Band Audio Spectrum Equalizer */
- audioSpectrum(
- signalChannel: string,
- minFreqHz = 60,
- maxFreqHz = 16000,
- maxScale = 2.5,
- ): this {
- this.props.audioSpectrum = {
- signalChannel,
- minFreqHz,
- maxFreqHz,
- scaleAxis: "scaleY",
- minScale: 0.2,
- maxScale,
- peakDecay: 0.8,
- };
- return this;
- }
-
- /** Long Shadow Extrusion */
- longShadow(
- angle = 45,
- length = 40,
- color = "#000000",
- startOpacity = 0.8,
- endOpacity = 0.0,
- steps = 16,
- ): this {
- this.props.longShadow = {
- angle,
- length,
- color,
- startOpacity,
- endOpacity,
- steps,
- };
- return this;
- }
-
- /** Particle Dust / Quantum Dissolve */
- particleDissolve(
- progress = 0,
- dispersalAngle = 90,
- maxDisplacement = 120,
- ): this {
- this.props.particleDissolve = {
- progress,
- dispersalAngle,
- maxDisplacement,
- turbulence: 0.5,
- particleSize: 2.0,
- };
- return this;
- }
-
- /** Add Expression Selector */
- addExpressionSelector(
- expression: string,
- mode: "add" | "subtract" | "intersect" = "add",
- ): this {
- this.selectors.push({
- type: "expression",
- expression,
- mode,
- amount: 1.0,
- });
- return this;
- }
-
- /** Alias for addExpressionSelector */
- expressionSelector(
- expression: string,
- mode: "add" | "subtract" | "intersect" = "add",
- ): this {
- return this.addExpressionSelector(expression, mode);
- }
-
- /** Standard 2D Transforms */
- x(v: number): this {
- this.props.x = v;
- return this;
- }
- y(v: number): this {
- this.props.y = v;
- return this;
- }
- scale(v: number): this {
- this.props.scale = v;
- return this;
- }
- scaleX(v: number): this {
- this.props.scaleX = v;
- return this;
- }
- scaleY(v: number): this {
- this.props.scaleY = v;
- return this;
- }
- rotation(deg: number): this {
- this.props.rotation = deg;
- return this;
- }
- opacity(v: number): this {
- this.props.opacity = v;
- return this;
- }
- blur(v: number): this {
- this.props.blur = v;
- return this;
- }
- fillColor(hex: string): this {
- this.props.fillColor = hex;
- return this;
- }
-
- build(): {
- name: string;
- selectors: (ExpressionSelector | Record<string, unknown>)[];
- props: ExtendedGlyphProperties;
- } {
- return {
- name: this.name,
- selectors: this.selectors,
- props: this.props,
- };
- }
-}
diff --git a/packages/compositions/src/program/text-animations/advanced-types.ts b/packages/compositions/src/program/text-animations/advanced-types.ts
deleted file mode 100644
index aeee67c2..00000000
--- a/packages/compositions/src/program/text-animations/advanced-types.ts
+++ /dev/null
@@ -1,381 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/advanced-types.ts
- * Data contracts and Zod schemas for Advanced Motion Design & After Effects Extended Typography Engine.
- */
-
-import { z } from "zod";
-
-// ==============================================================================================
-// 1. ANCHOR POINT GROUPING & CUSTOM PIVOT ALIGNMENT
-// ==============================================================================================
-
-export const AnchorGroupingSchema = z.enum([
- "character",
- "word",
- "line",
- "all",
-]);
-export type AnchorGrouping = z.infer<typeof AnchorGroupingSchema>;
-
-export const AnchorPresetSchema = z.enum([
- "center",
- "baseline",
- "bottom_left",
- "bottom_center",
- "bottom_right",
- "top_left",
- "top_center",
- "top_right",
- "custom",
-]);
-export type AnchorPreset = z.infer<typeof AnchorPresetSchema>;
-
-export const TextAnchorConfigSchema = z.object({
- /** Grouping scope */
- grouping: AnchorGroupingSchema.default("character"),
- /** Preset pivot location */
- preset: AnchorPresetSchema.default("center"),
- /** Normalized horizontal anchor point (0 = left, 0.5 = center, 1 = right) */
- anchorX: z.number().default(0.5),
- /** Normalized vertical anchor point (0 = top, 0.5 = middle, 1 = baseline/bottom) */
- anchorY: z.number().default(0.5),
- /** Pixel offset added to the calculated anchor point */
- offsetX: z.number().default(0),
- offsetY: z.number().default(0),
-});
-export type TextAnchorConfig = z.infer<typeof TextAnchorConfigSchema>;
-
-// ==============================================================================================
-// 2. DIRECTIONAL SKEW & SKEW AXIS
-// ==============================================================================================
-
-export const SkewPropertiesSchema = z.object({
- /** Skew angle in degrees (-90 to +90). Positive shears to the right. */
- skew: z.number().min(-90).max(90),
- /** Skew axis angle in degrees (0 to 360). 0 = horizontal shear, 90 = vertical shear. */
- skewAxis: z.number().default(0),
-});
-export type SkewProperties = z.infer<typeof SkewPropertiesSchema>;
-
-// ==============================================================================================
-// 3. DYNAMIC LINE SPACING (LEADING) & LINE ANCHORS
-// ==============================================================================================
-
-export const LineSpacingDynamicsSchema = z.object({
- /** Additional inter-line leading offset in pixels (can be negative for overlap). */
- lineSpacing: z.number(),
- /**
- * Normalized line anchor defining the stationary pivot line:
- * 0.0 = Top line remains stationary, subsequent lines push downwards.
- * 0.5 = Center line remains stationary, lines expand outward symmetrically (accordion).
- * 1.0 = Bottom line remains stationary, upper lines push upwards.
- */
- lineAnchor: z.number().min(0).max(1).default(0),
-});
-export type LineSpacingDynamics = z.infer<typeof LineSpacingDynamicsSchema>;
-
-// ==============================================================================================
-// 4. PER-CHARACTER 3D SPATIAL DEPTH & VOLUMETRIC FORMATIONS
-// ==============================================================================================
-
-export const VolumetricFormationModeSchema = z.enum([
- "planar", // Individual translateZ camera depth offset
- "vortex", // Logarithmic spiral into the screen along Z axis
- "cylinder", // Cylindrical drum carousel around horizontal or vertical axis
- "helix", // 3D corkscrew DNA helix twisting along an axis
-]);
-export type VolumetricFormationMode = z.infer<
- typeof VolumetricFormationModeSchema
->;
-
-export const VolumetricFormationConfigSchema = z.object({
- mode: VolumetricFormationModeSchema.default("planar"),
- /** Radius for cylinder / vortex / helix in pixels */
- radius: z.number().default(300),
- /** Pitch / axial spacing per revolution for helix / vortex in pixels */
- pitch: z.number().default(120),
- /** Total angular rotation across all characters in degrees */
- totalAngle: z.number().default(360),
- /** Cylinder rotation axis */
- axis: z.enum(["x", "y", "z"]).default("y"),
- /** Perspective distance in pixels (defaults to 1200) */
- perspective: z.number().positive().default(1200),
-});
-export type VolumetricFormationConfig = z.infer<
- typeof VolumetricFormationConfigSchema
->;
-
-export const Spatial3DPropertiesSchema = z.object({
- /** Translation along the camera Z axis (pixels; positive = towards camera) */
- translateZ: z.number().optional(),
- /** Rotation around the 3D X axis in degrees (flip-down pitch) */
- rotationX: z.number().optional(),
- /** Rotation around the 3D Y axis in degrees (card-turn yaw) */
- rotationY: z.number().optional(),
- /** Rotation around the 3D Z axis in degrees (planar roll) */
- rotationZ: z.number().optional(),
- /** Volumetric 3D arrangement configuration */
- formation: VolumetricFormationConfigSchema.optional(),
-});
-export type Spatial3DProperties = z.infer<typeof Spatial3DPropertiesSchema>;
-
-// ==============================================================================================
-// 5. INERTIAL SPRING & ELASTIC BOUNCE PHYSICS
-// ==============================================================================================
-
-export const SpringPhysicsSchema = z.object({
- /** Spring stiffness constant (k > 0). Higher values oscillate faster. Default: 180. */
- stiffness: z.number().positive().default(180),
- /** Damping coefficient (c > 0). Controls energy dissipation. Default: 12. */
- damping: z.number().positive().default(12),
- /** Mass of each glyph (m > 0). Heavier glyphs have more inertia. Default: 1.0. */
- mass: z.number().positive().default(1.0),
- /** Initial velocity imparted on the glyph upon selector contact. Default: 0. */
- initialVelocity: z.number().default(0),
- /** Optional elastic overshoot factor multiplier (> 1 boosts bounce). Default: 1.0. */
- overshootMultiplier: z.number().min(0.5).max(3.0).default(1.0),
-});
-export type SpringPhysics = z.infer<typeof SpringPhysicsSchema>;
-
-// ==============================================================================================
-// 6. EXPRESSION SELECTORS (PROGRAMMATIC PER-GLYPH EVALUATION)
-// ==============================================================================================
-
-export interface ExpressionEvaluationContext {
- /** 0-based index of the current character */
- charIndex: number;
- /** 0-based index of the current word */
- wordIndex: number;
- /** 0-based index of the current line */
- lineIndex: number;
- /** Total count of characters in paragraph */
- totalChars: number;
- /** Total count of words in paragraph */
- totalWords: number;
- /** Total count of lines in paragraph */
- totalLines: number;
- /** Current timeline frame number */
- frame: number;
- /** Timeline frame rate */
- fps: number;
- /** Current timeline timestamp in seconds */
- time: number;
- /** Bound reactive signals map */
- signals?: Record<string, unknown>;
-}
-
-export const ExpressionSelectorSchema = z.object({
- type: z.literal("expression"),
- name: z.string().optional(),
- /**
- * Mathematical formula expression string.
- * Available variables: textIndex, textTotal, wordIndex, wordTotal, lineIndex, lineTotal, time, frame, fps, sin, cos, abs, min, max, clamp, noise.
- */
- expression: z.string(),
- /** Selection combination mode with prior selectors in stack */
- mode: z
- .enum(["add", "subtract", "intersect", "min", "max", "difference"])
- .default("add"),
- /** Amount multiplier applied to output weight */
- amount: z.number().default(1.0),
-});
-export type ExpressionSelector = z.infer<typeof ExpressionSelectorSchema>;
-
-// ==============================================================================================
-// 7. DYNAMIC STROKE & WIREFRAME EXPANSION
-// ==============================================================================================
-
-export const DynamicStrokePropertiesSchema = z.object({
- /** Stroke outline thickness in pixels */
- strokeWidth: z.number().min(0).default(0),
- /** Stroke outline color in hex/rgb */
- strokeColor: z.string().default("#ffffff"),
- /** Stroke alignment relative to glyph boundary */
- strokeAlign: z.enum(["center", "inside", "outside"]).default("center"),
- /** Fill opacity (0 = transparent wireframe, 1 = fully filled solid) */
- fillOpacity: z.number().min(0).max(1).default(1.0),
- /** Dashed stroke pattern: [dashLength, gapLength] in pixels */
- dashArray: z.tuple([z.number(), z.number()]).optional(),
- /** Dashed stroke animated offset in pixels */
- dashOffset: z.number().default(0),
-});
-export type DynamicStrokeProperties = z.infer<
- typeof DynamicStrokePropertiesSchema
->;
-
-// ==============================================================================================
-// 8. INFINITE KINETIC MARQUEE & CYLINDRICAL DRUM (SLOT MACHINE)
-// ==============================================================================================
-
-export const KineticMarqueeConfigSchema = z.object({
- /** Motion flow direction */
- direction: z.enum(["left", "right", "up", "down"]).default("left"),
- /** Continuous scroll velocity in pixels per second */
- velocity: z.number().default(120),
- /** Seamless loop wrapping within container bounds */
- loop: z.boolean().default(true),
- /** Gap between looping text duplicates in pixels */
- repeatGap: z.number().default(80),
- /**
- * Slot Machine / Drum Snap:
- * When enabled, scrolling decelerates and snaps to discrete character or word boundaries.
- */
- snap: z
- .object({
- enabled: z.boolean().default(false),
- /** Granularity of snap stops */
- granularity: z.enum(["character", "word", "line"]).default("word"),
- /** Target stop index */
- stopIndex: z.number().default(0),
- /** Deceleration easing curve name */
- decelEase: z.string().default("cubic.out"),
- })
- .optional(),
-});
-export type KineticMarqueeConfig = z.infer<typeof KineticMarqueeConfigSchema>;
-
-// ==============================================================================================
-// 9. KINETIC VARIABLE FONT AXIS MODULATION
-// ==============================================================================================
-
-export const VariableFontAxesSchema = z.object({
- /** Weight axis ('wght'): 100 (Thin) to 900 (Black) */
- weight: z.number().min(100).max(1000).optional(),
- /** Width axis ('wdth'): 50% (Ultra-Condensed) to 200% (Ultra-Expanded) */
- width: z.number().min(50).max(200).optional(),
- /** Slant axis ('slnt'): -15 to +15 degrees */
- slant: z.number().min(-25).max(25).optional(),
- /** Italic binary axis ('ital'): 0 or 1 */
- italic: z.number().min(0).max(1).optional(),
- /** Optical size axis ('opsz'): 6 to 72 points */
- opticalSize: z.number().min(6).max(144).optional(),
-});
-export type VariableFontAxes = z.infer<typeof VariableFontAxesSchema>;
-
-// ==============================================================================================
-// 10. HIGH-END GPU & SHADER-NATIVE TYPOGRAPHY (VFX PAYLOAD)
-// ==============================================================================================
-
-export const ChromaticGlitchConfigSchema = z.object({
- /** Horizontal red-channel displacement offset in pixels */
- redShiftX: z.number().default(0),
- /** Vertical red-channel displacement offset in pixels */
- redShiftY: z.number().default(0),
- /** Horizontal blue-channel displacement offset in pixels */
- blueShiftX: z.number().default(0),
- /** Vertical blue-channel displacement offset in pixels */
- blueShiftY: z.number().default(0),
- /** Random slice scanline displacement probability per frame (0 to 1) */
- sliceProbability: z.number().min(0).max(1).default(0),
- /** Maximum slice pixel displacement */
- sliceAmplitude: z.number().default(0),
-});
-export type ChromaticGlitchConfig = z.infer<typeof ChromaticGlitchConfigSchema>;
-
-export const AudioSpectrumTypographySchema = z.object({
- /** Audio stem or FFT signal registry channel name */
- signalChannel: z.string(),
- /** Minimum audio frequency in Hz mapped to the first glyph */
- minFreqHz: z.number().default(60),
- /** Maximum audio frequency in Hz mapped to the last glyph */
- maxFreqHz: z.number().default(16000),
- /** Scale axis to modulate */
- scaleAxis: z.enum(["scaleY", "scaleX", "scale", "y"]).default("scaleY"),
- /** Minimum scale factor at silence */
- minScale: z.number().default(0.2),
- /** Maximum scale factor at peak magnitude */
- maxScale: z.number().default(2.5),
- /** Peak decay smoothing (0 = instantaneous, 0.9 = long decay) */
- peakDecay: z.number().min(0).max(0.99).default(0.8),
-});
-export type AudioSpectrumTypography = z.infer<
- typeof AudioSpectrumTypographySchema
->;
-
-export const LongShadowConfigSchema = z.object({
- /** Projection angle in degrees (e.g. 45 = bottom-right, 135 = bottom-left) */
- angle: z.number().default(45),
- /** Shadow extrusion length in pixels */
- length: z.number().min(0).default(0),
- /** Shadow color in hex/rgb */
- color: z.string().default("#000000"),
- /** Shadow start opacity (0 to 1) */
- startOpacity: z.number().min(0).max(1).default(0.8),
- /** Shadow terminal opacity (0 to 1, default 0 for soft fade) */
- endOpacity: z.number().min(0).max(1).default(0.0),
- /** Number of extrusion shadow slices (higher = smoother, 4 to 64) */
- steps: z.number().int().min(4).max(64).default(16),
-});
-export type LongShadowConfig = z.infer<typeof LongShadowConfigSchema>;
-
-export const ParticleDissolveConfigSchema = z.object({
- /** Dissolution progress (0 = solid glyph, 1 = fully disintegrated into particles) */
- progress: z.number().min(0).max(1).default(0),
- /** Dispersal directional angle in degrees (e.g. 90 = drifting upwards) */
- dispersalAngle: z.number().default(90),
- /** Maximum dispersal displacement distance in pixels */
- maxDisplacement: z.number().default(120),
- /** Turbulence noise frequency for erratic particle paths */
- turbulence: z.number().default(0.5),
- /** Particle size in pixels */
- particleSize: z.number().default(2.0),
-});
-export type ParticleDissolveConfig = z.infer<
- typeof ParticleDissolveConfigSchema
->;
-
-// ==============================================================================================
-// 11. UNIFIED EXTENDED ANIMATOR PROPERTY CONTRACT
-// ==============================================================================================
-
-export const ExtendedGlyphPropertiesSchema = z.object({
- // Spatial 2D & 3D
- x: z.number().optional(),
- y: z.number().optional(),
- translateZ: z.number().optional(),
- scale: z.number().optional(),
- scaleX: z.number().optional(),
- scaleY: z.number().optional(),
- rotation: z.number().optional(),
- rotationX: z.number().optional(),
- rotationY: z.number().optional(),
- rotationZ: z.number().optional(),
- /** Volumetric 3D arrangement (set by `volumetricFormation()`) */
- formation: VolumetricFormationConfigSchema.optional(),
- skew: z.number().optional(),
- skewAxis: z.number().optional(),
-
- // Typography & Metrics
- tracking: z.number().optional(),
- lineSpacing: z.number().optional(),
- lineAnchor: z.number().optional(),
- anchor: TextAnchorConfigSchema.optional(),
-
- // Optical & Material
- opacity: z.number().optional(),
- blur: z.number().optional(),
- fillColor: z.string().optional(),
- stroke: DynamicStrokePropertiesSchema.optional(),
-
- // Variable Fonts
- variableAxes: VariableFontAxesSchema.optional(),
-
- // GPU Shader VFX
- chromaticGlitch: ChromaticGlitchConfigSchema.optional(),
- longShadow: LongShadowConfigSchema.optional(),
- particleDissolve: ParticleDissolveConfigSchema.optional(),
- audioSpectrum: AudioSpectrumTypographySchema.optional(),
-
- // Dynamics & Physics
- spring: SpringPhysicsSchema.optional(),
-});
-export type ExtendedGlyphProperties = z.infer<
- typeof ExtendedGlyphPropertiesSchema
->;
-
-export const AdvancedTextAnimatorSchema = z.object({
- name: z.string().default("AdvancedAnimator"),
- selectors: z.array(z.record(z.string(), z.unknown())).default([]),
- props: ExtendedGlyphPropertiesSchema,
-});
-export type AdvancedTextAnimator = z.infer<typeof AdvancedTextAnimatorSchema>;
diff --git a/packages/compositions/src/program/text-animations/builder.ts b/packages/compositions/src/program/text-animations/builder.ts
deleted file mode 100644
index 31773085..00000000
--- a/packages/compositions/src/program/text-animations/builder.ts
+++ /dev/null
@@ -1,377 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/builder.ts
- * Fluent builders for Path-Following Text and After Effects Text Animators.
- */
-
-import {
- type AETextAnimator,
- AETextAnimatorSchema,
- type AnimatableGlyphProps,
- type CubicBezierSegment,
- type GlyphAnchorGrouping,
- type GlyphAnchorPoint,
- type SelectorBasedOn,
- type SelectorMode,
- type SelectorShape,
- type TextPathOptions,
- TextPathOptionsSchema,
- type TextSelector,
-} from "./types.js";
-
-/**
- * Fluent builder for creating Path-Following Text configurations.
- */
-export class TextPathBuilder {
- private options: Partial<TextPathOptions> = {
- firstMargin: 0,
- lastMargin: 0,
- reversePath: false,
- perpendicularToPath: true,
- forceAlignment: false,
- loop: false,
- baselineOffset: "baseline",
- baselineShift: 0,
- };
-
- static fromSvg(svgPath: string): TextPathBuilder {
- const builder = new TextPathBuilder();
- builder.options.path = { type: "svg", d: svgPath };
- return builder;
- }
-
- static fromBezier(segments: CubicBezierSegment[]): TextPathBuilder {
- const builder = new TextPathBuilder();
- builder.options.path = { type: "bezier", segments };
- return builder;
- }
-
- static fromCurve(curve: CubicBezierSegment): TextPathBuilder {
- return TextPathBuilder.fromBezier([curve]);
- }
-
- static circularBadge(
- cx: number,
- cy: number,
- radius: number,
- ): TextPathBuilder {
- const builder = new TextPathBuilder();
- builder.options.path = {
- type: "ellipse",
- cx,
- cy,
- rx: radius,
- ry: radius,
- startAngleDeg: 0,
- endAngleDeg: 360,
- };
- builder.options.loop = true;
- return builder;
- }
-
- static ellipse(
- cx: number,
- cy: number,
- rx: number,
- ry: number,
- startAngleDeg = 0,
- endAngleDeg = 360,
- ): TextPathBuilder {
- const builder = new TextPathBuilder();
- builder.options.path = {
- type: "ellipse",
- cx,
- cy,
- rx,
- ry,
- startAngleDeg,
- endAngleDeg,
- };
- builder.options.loop = Math.abs(endAngleDeg - startAngleDeg) >= 360;
- return builder;
- }
-
- static wave(
- startX: number,
- startY: number,
- length: number,
- amplitude: number,
- frequency: number,
- phaseDeg = 0,
- ): TextPathBuilder {
- const builder = new TextPathBuilder();
- builder.options.path = {
- type: "wave",
- startX,
- startY,
- length,
- amplitude,
- frequency,
- phaseDeg,
- };
- return builder;
- }
-
- firstMargin(margin: number): this {
- this.options.firstMargin = margin;
- return this;
- }
-
- lastMargin(margin: number): this {
- this.options.lastMargin = margin;
- return this;
- }
-
- reversePath(reverse: boolean): this {
- this.options.reversePath = reverse;
- return this;
- }
-
- perpendicularToPath(perpendicular: boolean): this {
- this.options.perpendicularToPath = perpendicular;
- return this;
- }
-
- forceAlignment(force: boolean): this {
- this.options.forceAlignment = force;
- return this;
- }
-
- loop(isLoop: boolean): this {
- this.options.loop = isLoop;
- return this;
- }
-
- baselineOffset(
- offset: "baseline" | "center" | "ascender" | "descender",
- ): this {
- this.options.baselineOffset = offset;
- return this;
- }
-
- baselineShift(shiftPixels: number): this {
- this.options.baselineShift = shiftPixels;
- return this;
- }
-
- build(): TextPathOptions {
- return TextPathOptionsSchema.parse(this.options);
- }
-}
-
-/**
- * Fluent builder for After Effects Range, Wiggly, and Signal Animators.
- */
-export class TextAnimatorBuilder {
- private name = "Animator";
- private selectors: TextSelector[] = [];
- private props: AnimatableGlyphProps = {};
- private anchor: GlyphAnchorPoint = {
- grouping: "character",
- anchorX: 0.5,
- anchorY: 0.8,
- };
-
- static create(name: string): TextAnimatorBuilder {
- const builder = new TextAnimatorBuilder();
- builder.name = name;
- return builder;
- }
-
- /** Adds an After Effects Range Selector */
- addRangeSelector(config: {
- start?: number;
- end?: number;
- offset?: number;
- shape?: SelectorShape;
- basedOn?: SelectorBasedOn;
- mode?: SelectorMode;
- easeHigh?: number;
- easeLow?: number;
- randomizeOrder?: boolean;
- randomSeed?: number;
- }): this {
- const rawHigh = config.easeHigh ?? 0;
- const easeHigh = Math.abs(rawHigh) > 1 ? rawHigh / 100 : rawHigh;
- const rawLow = config.easeLow ?? 0;
- const easeLow = Math.abs(rawLow) > 1 ? rawLow / 100 : rawLow;
-
- this.selectors.push({
- type: "range",
- start: config.start ?? 0,
- end: config.end ?? 1,
- offset: config.offset ?? 0,
- units: "percentage",
- basedOn: config.basedOn ?? "characters",
- shape: config.shape ?? "smooth",
- mode: config.mode ?? "add",
- amount: 1,
- easeHigh: Math.max(-1, Math.min(1, easeHigh)),
- easeLow: Math.max(-1, Math.min(1, easeLow)),
- randomizeOrder: config.randomizeOrder ?? false,
- randomSeed: config.randomSeed ?? 12345,
- });
- return this;
- }
-
- /** Adds an After Effects Wiggly Selector for procedural organic flutter */
- addWigglySelector(config: {
- wigglesPerSecond?: number;
- correlation?: number;
- minAmount?: number;
- maxAmount?: number;
- randomSeed?: number;
- mode?: SelectorMode;
- }): this {
- this.selectors.push({
- type: "wiggly",
- wigglesPerSecond: config.wigglesPerSecond ?? 2.0,
- correlation: config.correlation ?? 0.5,
- temporalPhaseDeg: 0,
- spatialPhaseDeg: 0,
- minAmount: config.minAmount ?? -1.0,
- maxAmount: config.maxAmount ?? 1.0,
- randomSeed: config.randomSeed ?? 42,
- basedOn: "characters",
- mode: config.mode ?? "intersect",
- });
- return this;
- }
-
- /** Adds a reactive signal selector */
- addSignalSelector(
- signalId: string,
- multiplier = 1.0,
- phaseSpread = 0.0,
- offset = 0.0,
- mode: SelectorMode = "add",
- ): this {
- this.selectors.push({
- type: "signal",
- signalId,
- multiplier,
- offset,
- phaseSpread,
- basedOn: "characters",
- mode,
- });
- return this;
- }
-
- // --- Animatable Target Properties ---
- x(pixels: number): this {
- this.props.x = pixels;
- return this;
- }
-
- y(pixels: number): this {
- this.props.y = pixels;
- return this;
- }
-
- z(pixels: number): this {
- this.props.z = pixels;
- return this;
- }
-
- scale(scaleValue: number): this {
- this.props.scale = scaleValue;
- return this;
- }
-
- scaleX(scaleValue: number): this {
- this.props.scaleX = scaleValue;
- return this;
- }
-
- scaleY(scaleValue: number): this {
- this.props.scaleY = scaleValue;
- return this;
- }
-
- rotation(degrees: number): this {
- this.props.rotation = degrees;
- return this;
- }
-
- rotationX(degrees: number): this {
- this.props.rotationX = degrees;
- return this;
- }
-
- rotationY(degrees: number): this {
- this.props.rotationY = degrees;
- return this;
- }
-
- skew(degrees: number): this {
- this.props.skew = degrees;
- return this;
- }
-
- skewAxis(degrees: number): this {
- this.props.skewAxis = degrees;
- return this;
- }
-
- opacity(value: number): this {
- this.props.opacity = value;
- return this;
- }
-
- blur(pixels: number): this {
- this.props.blur = pixels;
- return this;
- }
-
- fill(colorHex: string): this {
- return this.fillColor(colorHex);
- }
-
- fillColor(colorHex: string): this {
- this.props.fillColor = colorHex;
- return this;
- }
-
- strokeColor(colorHex: string): this {
- this.props.strokeColor = colorHex;
- return this;
- }
-
- strokeWidth(pixels: number): this {
- this.props.strokeWidth = pixels;
- return this;
- }
-
- tracking(spacingPixels: number): this {
- this.props.tracking = spacingPixels;
- return this;
- }
-
- lineSpacing(spacingPixels: number): this {
- this.props.lineSpacing = spacingPixels;
- return this;
- }
-
- characterOffset(offset: number): this {
- this.props.characterOffset = offset;
- return this;
- }
-
- anchorGrouping(
- grouping: GlyphAnchorGrouping,
- anchorX = 0.5,
- anchorY = 0.8,
- ): this {
- this.anchor = { grouping, anchorX, anchorY };
- return this;
- }
-
- build(): AETextAnimator {
- return AETextAnimatorSchema.parse({
- name: this.name,
- selectors: this.selectors,
- props: this.props,
- anchor: this.anchor,
- });
- }
-}
diff --git a/packages/compositions/src/program/text-animations/index.ts b/packages/compositions/src/program/text-animations/index.ts
deleted file mode 100644
index 0756e01c..00000000
--- a/packages/compositions/src/program/text-animations/index.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/index.ts
- * After Effects Parity Text Animation Engine (Kinetic Typography, Path-Following & Typewriter).
- */
-
-export * from "./advanced-builder.js";
-export * from "./advanced-types.js";
-export * from "./builder.js";
-export * from "./math.js";
-export * from "./path.js";
-export * from "./selectors.js";
-export * from "./types.js";
-export * from "./typewriter.js";
diff --git a/packages/compositions/src/program/text-animations/math.ts b/packages/compositions/src/program/text-animations/math.ts
deleted file mode 100644
index ce9b08c7..00000000
--- a/packages/compositions/src/program/text-animations/math.ts
+++ /dev/null
@@ -1,439 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/math.ts
- * Mathematical formulations, closed-form physics, affine matrices, and evaluation algorithms.
- */
-
-import type {
- AudioSpectrumTypography,
- ExpressionEvaluationContext,
- KineticMarqueeConfig,
- TextAnchorConfig,
- VolumetricFormationConfig,
-} from "./advanced-types.js";
-
-/**
- * Evaluates closed-form underdamped harmonic oscillator for spring physics:
- *
- * x(t) = target + (x0 - target) * exp(-zeta * omega_n * t) *
- * [ cos(omega_d * t) + (zeta / sqrt(1 - zeta^2)) * sin(omega_d * t) ]
- *
- * @param t Time in seconds since excitation
- * @param stiffness Spring stiffness (k)
- * @param damping Damping coefficient (c)
- * @param mass Mass of glyph (m)
- * @param initialVelocity Initial velocity (v0)
- * @returns Settled scalar progress in [0, 1] with overshoot
- */
-export function evaluateSpringProgress(
- t: number,
- stiffness = 180,
- damping = 12,
- mass = 1.0,
- initialVelocity = 0,
-): number {
- if (t <= 0) return 0;
-
- const omega_n = Math.sqrt(Math.max(1e-4, stiffness) / Math.max(1e-4, mass)); // Natural frequency
- const zeta = damping / (2 * Math.sqrt(Math.max(1e-4, stiffness * mass))); // Damping ratio
-
- if (zeta < 1.0) {
- // Underdamped (oscillates and overshoots target)
- const omega_d = omega_n * Math.sqrt(1.0 - zeta * zeta); // Damped frequency
- const decay = Math.exp(-zeta * omega_n * t);
- const c1 = -1.0;
- const c2 =
- (initialVelocity - zeta * omega_n * -1.0) / Math.max(1e-6, omega_d);
- const displacement =
- decay * (c1 * Math.cos(omega_d * t) + c2 * Math.sin(omega_d * t));
- return Math.min(2.0, Math.max(-1.0, 1.0 + displacement));
- } else if (Math.abs(zeta - 1.0) < 1e-4) {
- // Critically damped (fastest settle without overshoot)
- const decay = Math.exp(-omega_n * t);
- const displacement = (-1.0 + (initialVelocity - omega_n) * t) * decay;
- return 1.0 + displacement;
- } else {
- // Overdamped
- const s1 = -omega_n * (zeta - Math.sqrt(zeta * zeta - 1.0));
- const s2 = -omega_n * (zeta + Math.sqrt(zeta * zeta - 1.0));
- const c1 = (initialVelocity + s2) / Math.max(1e-6, s1 - s2);
- const c2 = -1.0 - c1;
- return 1.0 + (c1 * Math.exp(s1 * t) + c2 * Math.exp(s2 * t));
- }
-}
-
-/**
- * Evaluates 2D Directional Skew matrix components:
- *
- * S(theta, phi) = R(phi) * [ 1 tan(theta) ] * R(-phi)
- * [ 0 1 ]
- *
- * @param skewDeg Skew angle in degrees
- * @param skewAxisDeg Skew axis in degrees
- * @returns 2x2 affine shear matrix components [m00, m01, m10, m11]
- */
-export function evaluateSkewMatrix(
- skewDeg: number,
- skewAxisDeg = 0,
-): [number, number, number, number] {
- if (skewDeg === 0) return [1, 0, 0, 1];
-
- const theta = (skewDeg * Math.PI) / 180;
- const phi = (skewAxisDeg * Math.PI) / 180;
-
- const cosPhi = Math.cos(phi);
- const sinPhi = Math.sin(phi);
- const tanTheta = Math.tan(theta);
-
- // Multiplied rotation and shear matrices
- const m00 = 1 - tanTheta * cosPhi * sinPhi;
- const m01 = tanTheta * cosPhi * cosPhi;
- const m10 = -tanTheta * sinPhi * sinPhi;
- const m11 = 1 + tanTheta * sinPhi * cosPhi;
-
- return [m00, m01, m10, m11];
-}
-
-/**
- * Computes anchor pivot coordinates relative to glyph bounding box:
- *
- * @param bbox Glyph bounding box { width, height, ascender, descender }
- * @param config Text anchor configuration
- * @returns Pivot offset (px, py) relative to glyph quad center
- */
-export function computeAnchorPivotOffset(
- bbox: { width: number; height: number; ascender: number; descender: number },
- config: TextAnchorConfig,
-): { px: number; py: number } {
- let normX = config.anchorX;
- let normY = config.anchorY;
-
- switch (config.preset) {
- case "center":
- normX = 0.5;
- normY = 0.5;
- break;
- case "baseline":
- normX = 0.5;
- normY = bbox.height > 0 ? bbox.ascender / bbox.height : 0.8;
- break;
- case "bottom_left":
- normX = 0.0;
- normY = 1.0;
- break;
- case "bottom_center":
- normX = 0.5;
- normY = 1.0;
- break;
- case "bottom_right":
- normX = 1.0;
- normY = 1.0;
- break;
- case "top_left":
- normX = 0.0;
- normY = 0.0;
- break;
- case "top_center":
- normX = 0.5;
- normY = 0.0;
- break;
- case "top_right":
- normX = 1.0;
- normY = 0.0;
- break;
- default:
- normX = 0.5;
- normY = 0.5;
- break;
- }
-
- // Offset relative to quad center (-0.5 to +0.5 of quad dimensions)
- const px = (normX - 0.5) * bbox.width + config.offsetX;
- const py = (normY - 0.5) * bbox.height + config.offsetY;
-
- return { px, py };
-}
-
-/**
- * Computes leading displacement per line based on lineAnchor:
- *
- * @param lineIndex 0-based index of current line
- * @param lineCount Total lines in paragraph
- * @param lineSpacing Additional leading in pixels
- * @param lineAnchor Pivot line anchor in [0, 1]
- * @returns Y displacement in pixels
- */
-export function computeLineLeadingDisplacement(
- lineIndex: number,
- lineCount: number,
- lineSpacing: number,
- lineAnchor = 0,
-): number {
- if (lineSpacing === 0 || lineCount <= 1) return 0;
- const pivotIndex = lineAnchor * (lineCount - 1);
- return (lineIndex - pivotIndex) * lineSpacing;
-}
-
-/**
- * Evaluates an Expression Selector formula safely in a sandboxed numeric scope.
- *
- * @param expression Formula string (e.g. "Math.sin(time * 4 + textIndex * 0.2)")
- * @param ctx Glyph evaluation context
- * @returns Numeric selector weight clamped to [-1, 1]
- */
-export function evaluateExpressionSelector(
- expression: string,
- ctx: ExpressionEvaluationContext,
-): number {
- try {
- const scope = {
- textIndex: ctx.charIndex,
- textTotal: Math.max(1, ctx.totalChars),
- wordIndex: ctx.wordIndex,
- wordTotal: Math.max(1, ctx.totalWords),
- lineIndex: ctx.lineIndex,
- lineTotal: Math.max(1, ctx.totalLines),
- time: ctx.time,
- frame: ctx.frame,
- fps: ctx.fps,
- sin: Math.sin,
- cos: Math.cos,
- tan: Math.tan,
- exp: Math.exp,
- log: Math.log,
- abs: Math.abs,
- min: Math.min,
- max: Math.max,
- sqrt: Math.sqrt,
- pow: Math.pow,
- floor: Math.floor,
- ceil: Math.ceil,
- round: Math.round,
- PI: Math.PI,
- clamp: (v: number, lo: number, hi: number) =>
- Math.max(lo, Math.min(hi, v)),
- noise: (seed: number) => {
- const n = Math.sin(seed * 12.9898 + ctx.frame * 0.1) * 43758.5453;
- return (n - Math.floor(n)) * 2 - 1;
- },
- signals: ctx.signals ?? {},
- };
-
- const keys = Object.keys(scope);
- const values = Object.values(scope);
- const fn = new Function(...keys, `return (${expression});`);
- const result = Number(fn(...values));
-
- return Number.isFinite(result) ? Math.max(-1, Math.min(1, result)) : 0;
- } catch {
- return 0;
- }
-}
-
-/**
- * 3D Volumetric arrangement transformation output.
- */
-export interface Volumetric3DTransform {
- x: number;
- y: number;
- z: number;
- rotX: number;
- rotY: number;
- rotZ: number;
- scaleMult: number;
- depthOpacity?: number;
-}
-
-/**
- * Evaluates 3D Volumetric Formation parametric coordinates.
- */
-export function evaluateVolumetricFormation(
- glyphIndex: number,
- totalGlyphs: number,
- config: VolumetricFormationConfig,
-): Volumetric3DTransform {
- const n = Math.max(1, totalGlyphs);
- const norm = n > 1 ? glyphIndex / (n - 1) : 0;
- const totalAngleRad = (config.totalAngle * Math.PI) / 180;
- const phi = norm * totalAngleRad;
-
- let x = 0;
- let y = 0;
- let z = 0;
- let rotX = 0;
- let rotY = 0;
- let rotZ = 0;
- let depthOpacity = 1.0;
-
- switch (config.mode) {
- case "cylinder": {
- // Center arc around front-facing camera when totalAngle < 360; otherwise sweep full circle
- const theta =
- config.totalAngle < 360 ? (norm - 0.5) * totalAngleRad : phi;
- if (config.axis === "y") {
- x = config.radius * Math.sin(theta);
- z = config.radius * Math.cos(theta) - config.radius;
- rotY = (theta * 180) / Math.PI;
- } else if (config.axis === "x") {
- y = config.radius * Math.sin(theta);
- z = config.radius * Math.cos(theta) - config.radius;
- rotX = (theta * 180) / Math.PI;
- } else {
- x = config.radius * Math.sin(theta);
- y = config.radius * Math.cos(theta);
- rotZ = (theta * 180) / Math.PI;
- }
- const facing = Math.cos(theta);
- depthOpacity = Math.max(0.25, (facing + 1) * 0.5);
- break;
- }
- case "vortex": {
- const startAngleRad = -Math.PI / 2; // Start upright at 12 o'clock
- const spiralPhi = startAngleRad + phi;
- // Smooth radius decay from R down to 0.65 * R to keep inner characters cleanly separated
- const r = config.radius * (1 - norm * 0.35);
- x = r * Math.cos(spiralPhi);
- y = r * Math.sin(spiralPhi);
- z = -glyphIndex * config.pitch || 0;
-
- // Exact analytical tangent orientation along spiral curve
- const dr = (-0.35 * config.radius) / (totalAngleRad || 1);
- const dx = dr * Math.cos(spiralPhi) - r * Math.sin(spiralPhi);
- const dy = dr * Math.sin(spiralPhi) + r * Math.cos(spiralPhi);
- rotZ = (Math.atan2(dy, dx) * 180) / Math.PI;
-
- depthOpacity = Math.max(0.65, 1 - norm * 0.35);
- break;
- }
- case "helix": {
- if (config.axis === "x") {
- // Horizontal DNA ribbon advancing along reading axis X
- const pitchX = config.pitch > 10 ? config.pitch : 28;
- x = (glyphIndex - (n - 1) / 2) * pitchX;
- y = config.radius * Math.sin(phi);
- z = config.radius * Math.cos(phi) - config.radius;
- // Subtle 3D wave tilt (max 22°) along surface normal
- rotX = Math.sin(phi) * 22;
- const facing = Math.cos(phi);
- depthOpacity = 0.75 + 0.25 * ((facing + 1) * 0.5);
- } else {
- // Vertical helix advancing along Y
- const theta = (norm - 0.5) * totalAngleRad;
- x = config.radius * Math.sin(theta);
- y = (glyphIndex - (n - 1) / 2) * config.pitch;
- z = config.radius * Math.cos(theta) - config.radius;
- rotY = (theta * 180) / Math.PI;
- const facing = Math.cos(theta);
- depthOpacity = Math.max(0.65, (facing + 1) * 0.5);
- }
- break;
- }
- default: {
- z = 0;
- break;
- }
- }
-
- const perspective = Math.max(100, config.perspective ?? 1200);
- const depthDist = perspective - z;
- const scaleMult = depthDist > 1 ? perspective / depthDist : 1;
-
- return {
- x: x * scaleMult,
- y: y * scaleMult,
- z,
- rotX,
- rotY,
- rotZ,
- scaleMult: Math.max(0.1, scaleMult),
- depthOpacity,
- };
-}
-
-/**
- * Evaluates infinite kinetic marquee translation and snapping.
- */
-export function evaluateMarqueeOffset(
- baseX: number,
- containerWidth: number,
- contentWidth: number,
- config: KineticMarqueeConfig,
- elapsedSec: number,
-): number {
- const effectiveWidth =
- containerWidth > 0 ? Math.max(containerWidth, contentWidth) : contentWidth;
- const totalSpan = effectiveWidth + config.repeatGap;
- if (totalSpan <= 0) return baseX;
-
- let travel = config.velocity * elapsedSec;
-
- if (config.snap?.enabled) {
- const targetStop = config.snap.stopIndex ?? 0;
- const snapUnit = config.snap.granularity === "character" ? 40 : 120;
- const targetPos = targetStop * snapUnit;
- const decelFactor = Math.min(1.0, elapsedSec * 1.5);
- travel = travel * (1.0 - decelFactor) + targetPos * decelFactor;
- }
-
- if (config.direction === "right" || config.direction === "down") {
- travel = -travel;
- }
-
- if (config.loop) {
- const wrapped = (baseX - travel) % totalSpan;
- return wrapped < -contentWidth ? wrapped + totalSpan : wrapped;
- }
-
- return baseX - travel;
-}
-
-/**
- * Samples audio FFT frequency bin and computes scale factor for Audio Spectrum Typography.
- */
-export function sampleAudioSpectrumScale(
- glyphIndex: number,
- totalGlyphs: number,
- config: AudioSpectrumTypography,
- signals: Record<string, unknown> | undefined,
- frame: number,
- fps: number,
-): number {
- const n = Math.max(1, totalGlyphs);
- const norm = n > 1 ? glyphIndex / (n - 1) : 0.5;
-
- const minFreq = Math.max(20, config.minFreqHz);
- const maxFreq = Math.max(minFreq + 100, config.maxFreqHz);
- const targetFreq = minFreq * (maxFreq / minFreq) ** norm;
-
- let magnitude = 0;
-
- if (signals) {
- const sig = signals[config.signalChannel];
- if (typeof sig === "number") {
- magnitude = sig;
- } else if (typeof sig === "function") {
- magnitude = Number(sig(frame, fps, targetFreq)) || 0;
- } else if (typeof sig === "object" && sig !== null) {
- const sObj = sig as Record<string, unknown>;
- if (typeof sObj.get === "function") {
- magnitude =
- Number(
- (sObj.get as (ctx: unknown) => unknown)({
- frame,
- fps,
- freq: targetFreq,
- }),
- ) || 0;
- } else if (
- Array.isArray(sObj.samples) ||
- ArrayBuffer.isView(sObj.samples)
- ) {
- const arr = sObj.samples as ArrayLike<number>;
- const binIdx = Math.floor(norm * (arr.length - 1));
- magnitude = Number(arr[binIdx]) || 0;
- }
- }
- }
-
- const clampedMag = Math.max(0, Math.min(1, magnitude));
- return config.minScale + clampedMag * (config.maxScale - config.minScale);
-}
diff --git a/packages/compositions/src/program/text-animations/path.ts b/packages/compositions/src/program/text-animations/path.ts
deleted file mode 100644
index 8b26298f..00000000
--- a/packages/compositions/src/program/text-animations/path.ts
+++ /dev/null
@@ -1,838 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/path.ts
- * Mathematical curve parameterization, Arc-Length LUTs, and path generators.
- */
-
-import type { CubicBezierSegment, TextPathSource, Vec2 } from "./types.js";
-
-export interface ArcLengthLUTEntry {
- distance: number;
- t: number;
-}
-
-export interface PathChainLUTEntry {
- distance: number;
- segmentIndex: number;
- t: number;
-}
-
-export interface PathSegmentLUT {
- segmentIndex: number;
- startDistance: number;
- length: number;
- lut: ArcLengthLUTEntry[];
-}
-
-export interface PathChainLUT {
- totalLength: number;
- entries: PathChainLUTEntry[];
- segments?: PathSegmentLUT[];
-}
-
-/**
- * Evaluates a point on a cubic Bezier curve at parameter t in [0, 1].
- */
-export function evaluateCubicBezier(
- segment: CubicBezierSegment,
- t: number,
-): Vec2 {
- const u = 1 - t;
- const tt = t * t;
- const uu = u * u;
- const uuu = uu * u;
- const ttt = tt * t;
-
- return {
- x:
- uuu * segment.p0.x +
- 3 * uu * t * segment.p1.x +
- 3 * u * tt * segment.p2.x +
- ttt * segment.p3.x,
- y:
- uuu * segment.p0.y +
- 3 * uu * t * segment.p1.y +
- 3 * u * tt * segment.p2.y +
- ttt * segment.p3.y,
- };
-}
-
-/**
- * Evaluates the first derivative (tangent velocity vector) of a cubic Bezier curve at parameter t.
- */
-export function evaluateCubicBezierDerivative(
- segment: CubicBezierSegment,
- t: number,
-): Vec2 {
- const u = 1 - t;
- return {
- x:
- 3 * u * u * (segment.p1.x - segment.p0.x) +
- 6 * u * t * (segment.p2.x - segment.p1.x) +
- 3 * t * t * (segment.p3.x - segment.p2.x),
- y:
- 3 * u * u * (segment.p1.y - segment.p0.y) +
- 6 * u * t * (segment.p2.y - segment.p1.y) +
- 3 * t * t * (segment.p3.y - segment.p2.y),
- };
-}
-
-/**
- * Generates an arc-length parameterization lookup table (LUT) for a cubic Bezier segment.
- * Ensures glyphs travel with uniform speed and do not bunch up on tight curve bends.
- */
-export function generateArcLengthLUT(
- segment: CubicBezierSegment,
- samples = 100,
-): { lut: ArcLengthLUTEntry[]; totalLength: number } {
- const lut: ArcLengthLUTEntry[] = [{ distance: 0, t: 0 }];
- let accumulatedDist = 0;
- let prevPoint = segment.p0;
-
- for (let i = 1; i <= samples; i++) {
- const t = i / samples;
- const currentPoint = evaluateCubicBezier(segment, t);
- const dx = currentPoint.x - prevPoint.x;
- const dy = currentPoint.y - prevPoint.y;
- const dist = Math.hypot(dx, dy);
- accumulatedDist += dist;
- lut.push({ distance: accumulatedDist, t });
- prevPoint = currentPoint;
- }
-
- return { lut, totalLength: accumulatedDist };
-}
-
-/**
- * Maps a distance along the curve (in pixels) to normalized curve parameter t via binary search on the LUT.
- */
-export function distanceToCurveParameter(
- lut: ArcLengthLUTEntry[],
- distance: number,
- totalLength: number,
-): number {
- if (distance <= 0) return 0;
- if (distance >= totalLength) return 1;
-
- let low = 0;
- let high = lut.length - 1;
-
- while (low <= high) {
- const mid = (low + high) >> 1;
- if (lut[mid].distance < distance) {
- low = mid + 1;
- } else {
- high = mid - 1;
- }
- }
-
- const index = Math.max(1, Math.min(low, lut.length - 1));
- const prev = lut[index - 1];
- const curr = lut[index];
- const segmentDist = curr.distance - prev.distance;
-
- if (segmentDist <= 1e-6) return prev.t;
- const ratio = (distance - prev.distance) / segmentDist;
- return prev.t + ratio * (curr.t - prev.t);
-}
-
-/**
- * Samples position and baseline tangent orientation angle along a curve at distance `dist`.
- */
-export function sampleCurveGeometry(
- segment: CubicBezierSegment,
- lut: ArcLengthLUTEntry[],
- totalLength: number,
- distance: number,
-): { position: Vec2; tangentAngleDeg: number; normal: Vec2 } {
- const t = distanceToCurveParameter(lut, distance, totalLength);
- const pos = evaluateCubicBezier(segment, t);
- const deriv = evaluateCubicBezierDerivative(segment, t);
- const length = Math.hypot(deriv.x, deriv.y);
- const nx = length > 1e-6 ? -deriv.y / length : 0;
- const ny = length > 1e-6 ? deriv.x / length : 1;
- const tangentAngleDeg = (Math.atan2(deriv.y, deriv.x) * 180) / Math.PI;
-
- return {
- position: pos,
- tangentAngleDeg,
- normal: { x: nx, y: ny },
- };
-}
-
-/**
- * Builds a cumulative Arc-Length LUT across a chain of Cubic Bezier segments.
- */
-export function buildPathLUT(
- segments: CubicBezierSegment[],
- samplesPerSegment = 50,
-): PathChainLUT {
- if (segments.length === 0) {
- return {
- totalLength: 0,
- entries: [{ distance: 0, segmentIndex: 0, t: 0 }],
- segments: [],
- };
- }
-
- const segLuts: PathSegmentLUT[] = [];
- const entries: PathChainLUTEntry[] = [];
- let accumulatedDist = 0;
-
- for (let segIdx = 0; segIdx < segments.length; segIdx++) {
- const seg = segments[segIdx];
- const { lut: segLut, totalLength: segLength } = generateArcLengthLUT(
- seg,
- samplesPerSegment,
- );
-
- segLuts.push({
- segmentIndex: segIdx,
- startDistance: accumulatedDist,
- length: segLength,
- lut: segLut,
- });
-
- for (let i = 0; i < segLut.length; i++) {
- entries.push({
- distance: accumulatedDist + segLut[i].distance,
- segmentIndex: segIdx,
- t: segLut[i].t,
- });
- }
-
- accumulatedDist += segLength;
- }
-
- return {
- totalLength: accumulatedDist,
- entries,
- segments: segLuts,
- };
-}
-
-/**
- * Samples position, tangent angle, and normal across a chain of Cubic Bezier segments.
- */
-export function samplePathChainGeometry(
- segments: CubicBezierSegment[],
- chainLUT: PathChainLUT,
- distance: number,
-): { position: Vec2; tangentAngleDeg: number; normal: Vec2 } {
- if (segments.length === 0) {
- return {
- position: { x: 0, y: 0 },
- tangentAngleDeg: 0,
- normal: { x: 0, y: 1 },
- };
- }
-
- const totalLength = chainLUT.totalLength;
-
- if (distance <= 0) {
- const seg = segments[0];
- const deriv = evaluateCubicBezierDerivative(seg, 0);
- const length = Math.hypot(deriv.x, deriv.y);
- const ux = length > 1e-6 ? deriv.x / length : 1;
- const uy = length > 1e-6 ? deriv.y / length : 0;
- const pos = {
- x: seg.p0.x + ux * distance,
- y: seg.p0.y + uy * distance,
- };
- const nx = length > 1e-6 ? -deriv.y / length : 0;
- const ny = length > 1e-6 ? deriv.x / length : 1;
- return {
- position: pos,
- tangentAngleDeg: (Math.atan2(deriv.y, deriv.x) * 180) / Math.PI,
- normal: { x: nx, y: ny },
- };
- }
-
- if (distance >= totalLength) {
- const lastSeg = segments[segments.length - 1];
- const deriv = evaluateCubicBezierDerivative(lastSeg, 1);
- const length = Math.hypot(deriv.x, deriv.y);
- const ux = length > 1e-6 ? deriv.x / length : 1;
- const uy = length > 1e-6 ? deriv.y / length : 0;
- const overflow = distance - totalLength;
- const pos = {
- x: lastSeg.p3.x + ux * overflow,
- y: lastSeg.p3.y + uy * overflow,
- };
- const nx = length > 1e-6 ? -deriv.y / length : 0;
- const ny = length > 1e-6 ? deriv.x / length : 1;
- return {
- position: pos,
- tangentAngleDeg: (Math.atan2(deriv.y, deriv.x) * 180) / Math.PI,
- normal: { x: nx, y: ny },
- };
- }
-
- let segIdx = 0;
- let t = 0;
-
- if (chainLUT.segments && chainLUT.segments.length > 0) {
- let low = 0;
- let high = chainLUT.segments.length - 1;
- while (low <= high) {
- const mid = (low + high) >> 1;
- const segLUT = chainLUT.segments[mid];
- if (distance < segLUT.startDistance) {
- high = mid - 1;
- } else if (distance > segLUT.startDistance + segLUT.length) {
- low = mid + 1;
- } else {
- low = mid;
- break;
- }
- }
- const sIdx = Math.max(0, Math.min(low, chainLUT.segments.length - 1));
- const segLUT = chainLUT.segments[sIdx];
- segIdx = segLUT.segmentIndex;
- const localDist = distance - segLUT.startDistance;
- t = distanceToCurveParameter(segLUT.lut, localDist, segLUT.length);
- } else {
- let low = 0;
- let high = chainLUT.entries.length - 1;
- while (low <= high) {
- const mid = (low + high) >> 1;
- if (chainLUT.entries[mid].distance < distance) {
- low = mid + 1;
- } else {
- high = mid - 1;
- }
- }
- const index = Math.max(1, Math.min(low, chainLUT.entries.length - 1));
- const prev = chainLUT.entries[index - 1];
- const curr = chainLUT.entries[index];
- const segmentDist = curr.distance - prev.distance;
- const ratio =
- segmentDist > 1e-6 ? (distance - prev.distance) / segmentDist : 0;
- segIdx = curr.segmentIndex;
- t =
- prev.segmentIndex === curr.segmentIndex
- ? prev.t + ratio * (curr.t - prev.t)
- : curr.t;
- }
-
- const segment = segments[segIdx] ?? segments[0];
- const pos = evaluateCubicBezier(segment, t);
- const deriv = evaluateCubicBezierDerivative(segment, t);
- const length = Math.hypot(deriv.x, deriv.y);
- const nx = length > 1e-6 ? -deriv.y / length : 0;
- const ny = length > 1e-6 ? deriv.x / length : 1;
- const tangentAngleDeg = (Math.atan2(deriv.y, deriv.x) * 180) / Math.PI;
-
- return {
- position: pos,
- tangentAngleDeg,
- normal: { x: nx, y: ny },
- };
-}
-
-/**
- * Converts an ellipse arc into cubic Bezier segments (up to 90 degrees per segment).
- */
-export function ellipseToCubicBeziers(
- cx: number,
- cy: number,
- rx: number,
- ry: number,
- startAngleDeg = 0,
- endAngleDeg = 360,
-): CubicBezierSegment[] {
- const startRad = (startAngleDeg * Math.PI) / 180;
- const endRad = (endAngleDeg * Math.PI) / 180;
- const totalSweep = endRad - startRad;
-
- // Keep sign, split into chunks of at most pi/2 (90 deg)
- const numSegments = Math.max(
- 1,
- Math.ceil(Math.abs(totalSweep) / (Math.PI / 2)),
- );
- const sweepStep = totalSweep / numSegments;
- const segments: CubicBezierSegment[] = [];
-
- for (let i = 0; i < numSegments; i++) {
- const a0 = startRad + i * sweepStep;
- const a1 = a0 + sweepStep;
- const halfSweep = sweepStep / 2;
- // Standard constant for cubic approximation of circular/elliptical arc
- const k = (4 / 3) * Math.tan(halfSweep / 2);
-
- const cos0 = Math.cos(a0);
- const sin0 = Math.sin(a0);
- const cos1 = Math.cos(a1);
- const sin1 = Math.sin(a1);
-
- const p0: Vec2 = { x: cx + rx * cos0, y: cy + ry * sin0 };
- const p1: Vec2 = { x: p0.x - k * rx * sin0, y: p0.y + k * ry * cos0 };
- const p3: Vec2 = { x: cx + rx * cos1, y: cy + ry * sin1 };
- const p2: Vec2 = { x: p3.x + k * rx * sin1, y: p3.y - k * ry * cos1 };
-
- segments.push({ p0, p1, p2, p3 });
- }
-
- return segments;
-}
-
-/**
- * Converts a sinusoidal wave into cubic Bezier segments per half-cycle.
- */
-export function waveToCubicBeziers(
- startX: number,
- startY: number,
- length: number,
- amplitude: number,
- frequency: number,
- phaseDeg = 0,
-): CubicBezierSegment[] {
- const phaseRad = (phaseDeg * Math.PI) / 180;
- const totalCycles =
- frequency < 0.1 && frequency * length >= 0.25
- ? frequency * length
- : Math.max(0.5, frequency);
- const halfCycles = Math.max(1, Math.round(totalCycles * 2));
- const dx = length / halfCycles;
- const segments: CubicBezierSegment[] = [];
-
- for (let i = 0; i < halfCycles; i++) {
- const t0 = (i / halfCycles) * (2 * Math.PI * totalCycles) + phaseRad;
- const t1 = ((i + 1) / halfCycles) * (2 * Math.PI * totalCycles) + phaseRad;
-
- const x0 = startX + i * dx;
- const x3 = startX + (i + 1) * dx;
- const y0 = startY + amplitude * Math.sin(t0);
- const y3 = startY + amplitude * Math.sin(t1);
-
- const slope0 = amplitude * Math.cos(t0) * (Math.PI / 3);
- const slope1 = amplitude * Math.cos(t1) * (Math.PI / 3);
-
- const p0: Vec2 = { x: x0, y: y0 };
- const p1: Vec2 = { x: x0 + dx / 3, y: y0 + slope0 };
- const p2: Vec2 = { x: x3 - dx / 3, y: y3 - slope1 };
- const p3: Vec2 = { x: x3, y: y3 };
-
- segments.push({ p0, p1, p2, p3 });
- }
-
- return segments;
-}
-
-/**
- * An SVG elliptical arc (endpoint form, SVG 1.1 F.6.5) as cubic Bezier
- * segments of at most 90° each.
- */
-export function svgArcToCubicBeziers(
- x1: number,
- y1: number,
- rxIn: number,
- ryIn: number,
- rotationDeg: number,
- largeArc: boolean,
- sweep: boolean,
- x2: number,
- y2: number,
-): CubicBezierSegment[] {
- if (x1 === x2 && y1 === y2) return [];
- let rx = Math.abs(rxIn);
- let ry = Math.abs(ryIn);
- if (rx === 0 || ry === 0) {
- return [
- {
- p0: { x: x1, y: y1 },
- p1: { x: x1 + (x2 - x1) / 3, y: y1 + (y2 - y1) / 3 },
- p2: { x: x1 + (2 * (x2 - x1)) / 3, y: y1 + (2 * (y2 - y1)) / 3 },
- p3: { x: x2, y: y2 },
- },
- ];
- }
- const phi = (rotationDeg * Math.PI) / 180;
- const cosPhi = Math.cos(phi);
- const sinPhi = Math.sin(phi);
- // Step 1: the midpoint in the ellipse's own axes.
- const dx = (x1 - x2) / 2;
- const dy = (y1 - y2) / 2;
- const x1p = cosPhi * dx + sinPhi * dy;
- const y1p = -sinPhi * dx + cosPhi * dy;
- // Radii too small to reach the endpoint scale up (F.6.6).
- const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
- if (lambda > 1) {
- rx *= Math.sqrt(lambda);
- ry *= Math.sqrt(lambda);
- }
- // Step 2: the centre in the ellipse's axes, then in user space.
- const num = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
- const den = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
- const coef = (largeArc !== sweep ? 1 : -1) * Math.sqrt(Math.max(0, num / den));
- const cxp = (coef * rx * y1p) / ry;
- const cyp = (-coef * ry * x1p) / rx;
- const cx = cosPhi * cxp - sinPhi * cyp + (x1 + x2) / 2;
- const cy = sinPhi * cxp + cosPhi * cyp + (y1 + y2) / 2;
- // Step 3: start angle and sweep.
- const angle = (ux: number, uy: number, vx: number, vy: number) =>
- Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
- const theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
- let delta = angle(
- (x1p - cxp) / rx,
- (y1p - cyp) / ry,
- (-x1p - cxp) / rx,
- (-y1p - cyp) / ry,
- );
- if (!sweep && delta > 0) delta -= 2 * Math.PI;
- if (sweep && delta < 0) delta += 2 * Math.PI;
-
- const n = Math.max(1, Math.ceil(Math.abs(delta) / (Math.PI / 2)));
- const step = delta / n;
- const k = (4 / 3) * Math.tan(step / 4);
- const point = (a: number): Vec2 => ({
- x: cx + rx * Math.cos(a) * cosPhi - ry * Math.sin(a) * sinPhi,
- y: cy + rx * Math.cos(a) * sinPhi + ry * Math.sin(a) * cosPhi,
- });
- const tangent = (a: number): Vec2 => ({
- x: -rx * Math.sin(a) * cosPhi - ry * Math.cos(a) * sinPhi,
- y: -rx * Math.sin(a) * sinPhi + ry * Math.cos(a) * cosPhi,
- });
- const segments: CubicBezierSegment[] = [];
- for (let s = 0; s < n; s++) {
- const a0 = theta1 + s * step;
- const a1 = a0 + step;
- const p0 = point(a0);
- const p3 = point(a1);
- const t0 = tangent(a0);
- const t1 = tangent(a1);
- segments.push({
- p0,
- p1: { x: p0.x + k * t0.x, y: p0.y + k * t0.y },
- p2: { x: p3.x - k * t1.x, y: p3.y - k * t1.y },
- p3,
- });
- }
- // Land exactly on the endpoint the path asked for.
- segments[segments.length - 1]!.p3 = { x: x2, y: y2 };
- return segments;
-}
-
-/**
- * Converts SVG path data string into a chain of Cubic Bezier segments.
- */
-export function parseSvgPathToCubicBeziers(d: string): CubicBezierSegment[] {
- const segments: CubicBezierSegment[] = [];
- const tokens = d.match(/[a-df-z]|[-+]?(?:\d*\.\d+|\d+)(?:[eE][-+]?\d+)?/gi);
- if (!tokens || tokens.length === 0) return segments;
-
- let currentX = 0;
- let currentY = 0;
- let startX = 0;
- let startY = 0;
- let lastControlX = 0;
- let lastControlY = 0;
- let lastCommand = "";
-
- let i = 0;
- const nextNum = (): number => {
- if (i >= tokens.length) return 0;
- return Number.parseFloat(tokens[i++]);
- };
-
- while (i < tokens.length) {
- const token = tokens[i++];
- const isCommand = /^[a-df-z]$/i.test(token);
- const cmd = isCommand ? token : lastCommand;
- if (!isCommand) i--; // put number back if repeated coords
-
- switch (cmd) {
- case "M": {
- currentX = nextNum();
- currentY = nextNum();
- startX = currentX;
- startY = currentY;
- lastCommand = "L";
- break;
- }
- case "m": {
- currentX += nextNum();
- currentY += nextNum();
- startX = currentX;
- startY = currentY;
- lastCommand = "l";
- break;
- }
- case "L": {
- const x = nextNum();
- const y = nextNum();
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: {
- x: currentX + (x - currentX) / 3,
- y: currentY + (y - currentY) / 3,
- },
- p2: {
- x: currentX + (2 * (x - currentX)) / 3,
- y: currentY + (2 * (y - currentY)) / 3,
- },
- p3: { x, y },
- });
- currentX = x;
- currentY = y;
- lastCommand = "L";
- break;
- }
- case "l": {
- const dx = nextNum();
- const dy = nextNum();
- const x = currentX + dx;
- const y = currentY + dy;
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: currentX + dx / 3, y: currentY + dy / 3 },
- p2: { x: currentX + (2 * dx) / 3, y: currentY + (2 * dy) / 3 },
- p3: { x, y },
- });
- currentX = x;
- currentY = y;
- lastCommand = "l";
- break;
- }
- case "H": {
- const x = nextNum();
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: currentX + (x - currentX) / 3, y: currentY },
- p2: { x: currentX + (2 * (x - currentX)) / 3, y: currentY },
- p3: { x, y: currentY },
- });
- currentX = x;
- lastCommand = "H";
- break;
- }
- case "h": {
- const dx = nextNum();
- const x = currentX + dx;
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: currentX + dx / 3, y: currentY },
- p2: { x: currentX + (2 * dx) / 3, y: currentY },
- p3: { x, y: currentY },
- });
- currentX = x;
- lastCommand = "h";
- break;
- }
- case "V": {
- const y = nextNum();
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: currentX, y: currentY + (y - currentY) / 3 },
- p2: { x: currentX, y: currentY + (2 * (y - currentY)) / 3 },
- p3: { x: currentX, y },
- });
- currentY = y;
- lastCommand = "V";
- break;
- }
- case "v": {
- const dy = nextNum();
- const y = currentY + dy;
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: currentX, y: currentY + dy / 3 },
- p2: { x: currentX, y: currentY + (2 * dy) / 3 },
- p3: { x: currentX, y },
- });
- currentY = y;
- lastCommand = "v";
- break;
- }
- case "C": {
- const x1 = nextNum();
- const y1 = nextNum();
- const x2 = nextNum();
- const y2 = nextNum();
- const x = nextNum();
- const y = nextNum();
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: x1, y: y1 },
- p2: { x: x2, y: y2 },
- p3: { x, y },
- });
- lastControlX = x2;
- lastControlY = y2;
- currentX = x;
- currentY = y;
- lastCommand = "C";
- break;
- }
- case "c": {
- const dx1 = nextNum();
- const dy1 = nextNum();
- const dx2 = nextNum();
- const dy2 = nextNum();
- const dx = nextNum();
- const dy = nextNum();
- const x1 = currentX + dx1;
- const y1 = currentY + dy1;
- const x2 = currentX + dx2;
- const y2 = currentY + dy2;
- const x = currentX + dx;
- const y = currentY + dy;
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: x1, y: y1 },
- p2: { x: x2, y: y2 },
- p3: { x, y },
- });
- lastControlX = x2;
- lastControlY = y2;
- currentX = x;
- currentY = y;
- lastCommand = "c";
- break;
- }
- case "S":
- case "s": {
- const isRel = cmd === "s";
- const dx2 = nextNum();
- const dy2 = nextNum();
- const dx = nextNum();
- const dy = nextNum();
- const x2 = isRel ? currentX + dx2 : dx2;
- const y2 = isRel ? currentY + dy2 : dy2;
- const x = isRel ? currentX + dx : dx;
- const y = isRel ? currentY + dy : dy;
-
- // Reflect last control point if previous was C/c/S/s
- let x1 = currentX;
- let y1 = currentY;
- if (/^[cs]$/i.test(lastCommand)) {
- x1 = 2 * currentX - lastControlX;
- y1 = 2 * currentY - lastControlY;
- }
-
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: x1, y: y1 },
- p2: { x: x2, y: y2 },
- p3: { x, y },
- });
- lastControlX = x2;
- lastControlY = y2;
- currentX = x;
- currentY = y;
- lastCommand = cmd;
- break;
- }
- case "Q":
- case "q": {
- const isRel = cmd === "q";
- const qx = nextNum();
- const qy = nextNum();
- const x = nextNum();
- const y = nextNum();
- const cx1 = isRel ? currentX + qx : qx;
- const cy1 = isRel ? currentY + qy : qy;
- const ex = isRel ? currentX + x : x;
- const ey = isRel ? currentY + y : y;
-
- // Quadratic to cubic conversion
- const p1x = currentX + (2 / 3) * (cx1 - currentX);
- const p1y = currentY + (2 / 3) * (cy1 - currentY);
- const p2x = ex + (2 / 3) * (cx1 - ex);
- const p2y = ey + (2 / 3) * (cy1 - ey);
-
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: { x: p1x, y: p1y },
- p2: { x: p2x, y: p2y },
- p3: { x: ex, y: ey },
- });
- lastControlX = cx1;
- lastControlY = cy1;
- currentX = ex;
- currentY = ey;
- lastCommand = cmd;
- break;
- }
- case "A":
- case "a": {
- const rx = nextNum();
- const ry = nextNum();
- const rotation = nextNum();
- const largeArc = nextNum() !== 0;
- const sweep = nextNum() !== 0;
- let x = nextNum();
- let y = nextNum();
- if (cmd === "a") {
- x += currentX;
- y += currentY;
- }
- segments.push(
- ...svgArcToCubicBeziers(currentX, currentY, rx, ry, rotation, largeArc, sweep, x, y),
- );
- currentX = x;
- currentY = y;
- lastControlX = x;
- lastControlY = y;
- lastCommand = cmd;
- break;
- }
- case "Z":
- case "z": {
- if (currentX !== startX || currentY !== startY) {
- segments.push({
- p0: { x: currentX, y: currentY },
- p1: {
- x: currentX + (startX - currentX) / 3,
- y: currentY + (startY - currentY) / 3,
- },
- p2: {
- x: currentX + (2 * (startX - currentX)) / 3,
- y: currentY + (2 * (startY - currentY)) / 3,
- },
- p3: { x: startX, y: startY },
- });
- currentX = startX;
- currentY = startY;
- }
- lastCommand = cmd;
- break;
- }
- default:
- break;
- }
- }
-
- return segments;
-}
-
-/**
- * Universal path source converter to Cubic Bezier segments.
- */
-export function generatePathSegments(
- source: TextPathSource,
-): CubicBezierSegment[] {
- switch (source.type) {
- case "bezier":
- return source.segments;
- case "svg":
- return parseSvgPathToCubicBeziers(source.d);
- case "ellipse":
- return ellipseToCubicBeziers(
- source.cx,
- source.cy,
- source.rx,
- source.ry,
- source.startAngleDeg ?? 0,
- source.endAngleDeg ?? 360,
- );
- case "wave":
- return waveToCubicBeziers(
- source.startX,
- source.startY,
- source.length,
- source.amplitude,
- source.frequency,
- source.phaseDeg ?? 0,
- );
- }
-}
diff --git a/packages/compositions/src/program/text-animations/selectors.ts b/packages/compositions/src/program/text-animations/selectors.ts
deleted file mode 100644
index a6784d16..00000000
--- a/packages/compositions/src/program/text-animations/selectors.ts
+++ /dev/null
@@ -1,151 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/selectors.ts
- * After Effects Range, Wiggly, and Signal selector weight evaluators.
- */
-
-import type { SelectorMode, SelectorShape } from "./types.js";
-
-/**
- * Computes After Effects Range Selector weight for a single glyph index.
- *
- * @param index Normalized position of the glyph in the string (0.0 to 1.0)
- * @param start Start boundary (0.0 to 1.0)
- * @param end End boundary (0.0 to 1.0)
- * @param offset Window shift (-1.0 to 1.0)
- * @param shape Falloff curve
- * @param easeHigh Non-linear easing for high values
- * @param easeLow Non-linear easing for low values
- * @returns Weight in [0.0, 1.0]
- */
-export function computeRangeSelectorWeight(
- index: number,
- start: number,
- end: number,
- offset: number,
- shape: SelectorShape,
- easeHigh = 0,
- easeLow = 0,
-): number {
- const effStart = start + offset;
- const effEnd = end + offset;
-
- if (effEnd <= effStart) return 0;
-
- const relativePos = (index - effStart) / (effEnd - effStart);
-
- let baseWeight = 0;
- switch (shape) {
- case "square":
- baseWeight = relativePos >= 0 && relativePos <= 1 ? 1 : 0;
- break;
- case "ramp_up":
- baseWeight = Math.max(0, Math.min(1, relativePos));
- break;
- case "ramp_down":
- baseWeight = 1 - Math.max(0, Math.min(1, relativePos));
- break;
- case "triangle": {
- if (relativePos < 0 || relativePos > 1) {
- baseWeight = 0;
- } else if (relativePos < 0.5) {
- baseWeight = relativePos * 2;
- } else {
- baseWeight = (1 - relativePos) * 2;
- }
- break;
- }
- case "round": {
- // Half-cosine bell curve
- if (relativePos < 0 || relativePos > 1) {
- baseWeight = 0;
- } else {
- baseWeight = 0.5 - 0.5 * Math.cos(relativePos * 2 * Math.PI);
- }
- break;
- }
- case "smooth": {
- // Hermite smoothstep S-curve
- if (relativePos <= 0) baseWeight = 0;
- else if (relativePos >= 1) baseWeight = 1;
- else baseWeight = relativePos * relativePos * (3 - 2 * relativePos);
- break;
- }
- }
-
- // Apply Ease High / Ease Low non-linear power shaping
- if (baseWeight > 0 && baseWeight < 1) {
- if (easeHigh !== 0 && baseWeight > 0.5) {
- const norm = (baseWeight - 0.5) * 2;
- const power = easeHigh > 0 ? 1 + easeHigh * 3 : 1 / (1 - easeHigh * 3);
- baseWeight = 0.5 + 0.5 * norm ** power;
- }
- if (easeLow !== 0 && baseWeight <= 0.5) {
- const norm = baseWeight * 2;
- const power = easeLow > 0 ? 1 + easeLow * 3 : 1 / (1 - easeLow * 3);
- baseWeight = 0.5 * norm ** power;
- }
- }
-
- return Math.max(0, Math.min(1, baseWeight));
-}
-
-/**
- * 1D Gradient deterministic hash noise for Wiggly Selectors.
- */
-export function deterministicWigglyNoise(
- seed: number,
- glyphIndex: number,
- phase: number,
- correlation: number,
-): number {
- const coord =
- (glyphIndex * (1 - correlation) + correlation) * 12.9898 +
- phase +
- seed * 78.233;
- const raw = Math.sin(coord) * 43758.5453;
- return (raw - Math.floor(raw)) * 2 - 1; // Range [-1.0, 1.0]
-}
-
-/**
- * Deterministic pseudo-random number generator for seeded order shuffling.
- */
-export function getShuffledOrder(count: number, seed: number): number[] {
- let s = seed;
- const nextRand = () => {
- s = (s * 9301 + 49297) % 233280;
- return s / 233280;
- };
-
- const arr = Array.from({ length: count }, (_, i) => i);
- for (let i = arr.length - 1; i > 0; i--) {
- const j = Math.floor(nextRand() * (i + 1));
- const temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- return arr;
-}
-
-/**
- * Combines consecutive selector weights using After Effects Selector Mode logic.
- */
-export function evaluateSelectorCombination(
- prevWeight: number,
- currWeight: number,
- mode: SelectorMode,
-): number {
- switch (mode) {
- case "add":
- return Math.max(0, Math.min(1, prevWeight + currWeight));
- case "subtract":
- return Math.max(0, Math.min(1, prevWeight - currWeight));
- case "intersect":
- return prevWeight * currWeight;
- case "min":
- return Math.min(prevWeight, currWeight);
- case "max":
- return Math.max(prevWeight, currWeight);
- case "difference":
- return Math.abs(prevWeight - currWeight);
- }
-}
diff --git a/packages/compositions/src/program/text-animations/types.ts b/packages/compositions/src/program/text-animations/types.ts
deleted file mode 100644
index 92718477..00000000
--- a/packages/compositions/src/program/text-animations/types.ts
+++ /dev/null
@@ -1,300 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/types.ts
- * Data contracts and Zod schemas for After Effects Parity Text Animation Engine.
- */
-
-import { z } from "zod";
-
-// ==============================================================================================
-// 1. PATH-FOLLOWING TEXT DATA CONTRACTS & SCHEMAS
-// ==============================================================================================
-
-export const Vec2Schema = z.object({
- x: z.number(),
- y: z.number(),
-});
-export type Vec2 = z.infer<typeof Vec2Schema>;
-
-export const CubicBezierSegmentSchema = z.object({
- p0: Vec2Schema,
- p1: Vec2Schema,
- p2: Vec2Schema,
- p3: Vec2Schema,
-});
-export type CubicBezierSegment = z.infer<typeof CubicBezierSegmentSchema>;
-
-export const TextPathSourceSchema = z.discriminatedUnion("type", [
- z.object({
- type: z.literal("svg"),
- d: z.string().min(1, "SVG path string cannot be empty"),
- }),
- z.object({
- type: z.literal("bezier"),
- segments: z.array(CubicBezierSegmentSchema).min(1),
- }),
- z.object({
- type: z.literal("ellipse"),
- cx: z.number(),
- cy: z.number(),
- rx: z.number().positive(),
- ry: z.number().positive(),
- startAngleDeg: z.number().default(0),
- endAngleDeg: z.number().default(360),
- }),
- z.object({
- type: z.literal("wave"),
- startX: z.number(),
- startY: z.number(),
- length: z.number().positive(),
- amplitude: z.number(),
- frequency: z.number().positive(),
- phaseDeg: z.number().default(0),
- }),
-]);
-export type TextPathSource = z.infer<typeof TextPathSourceSchema>;
-
-export const TextPathOptionsSchema = z.object({
- path: TextPathSourceSchema,
- firstMargin: z.number().default(0),
- lastMargin: z.number().default(0),
- reversePath: z.boolean().default(false),
- perpendicularToPath: z.boolean().default(true),
- forceAlignment: z.boolean().default(false),
- loop: z.boolean().default(false),
- baselineOffset: z
- .enum(["baseline", "center", "ascender", "descender"])
- .default("baseline"),
- baselineShift: z.number().default(0),
-});
-export type TextPathOptions = z.infer<typeof TextPathOptionsSchema>;
-
-// ==============================================================================================
-// 2. TYPEWRITER & DISCRETE REVEAL SCHEMAS
-// ==============================================================================================
-
-export const TextCursorStyleSchema = z.enum([
- "bar",
- "underscore",
- "block",
- "custom",
-]);
-export type TextCursorStyle = z.infer<typeof TextCursorStyleSchema>;
-
-export const TextCursorConfigSchema = z.object({
- enabled: z.boolean().default(true),
- style: TextCursorStyleSchema.default("bar"),
- customChar: z.string().default("|"),
- color: z.string().optional(),
- blinkFrequency: z.number().min(0).default(2.0),
- hideOnComplete: z.boolean().default(false),
- width: z.number().positive().default(2.5),
- spacing: z.number().default(3.0),
-});
-export type TextCursorConfig = z.infer<typeof TextCursorConfigSchema>;
-
-export const TypingCadenceSchema = z.discriminatedUnion("mode", [
- z.object({
- mode: z.literal("constant"),
- }),
- z.object({
- mode: z.literal("human"),
- jitter: z.number().min(0).max(1).default(0.35),
- commaPauseMultiplier: z.number().min(1).default(3.0),
- sentencePauseMultiplier: z.number().min(1).default(5.5),
- paragraphPauseMultiplier: z.number().min(1).default(7.0),
- }),
-]);
-export type TypingCadence = z.infer<typeof TypingCadenceSchema>;
-
-export const TextScrambleConfigSchema = z.object({
- enabled: z.boolean().default(false),
- charset: z
- .enum(["ascii", "alphanumeric", "matrix", "binary", "hex", "custom"])
- .default("alphanumeric"),
- customCharset: z.string().optional(),
- scrambleFramesPerChar: z.number().int().min(1).default(8),
- highlightProbability: z.number().min(0).max(1).default(0.25),
- scrambleColor: z.string().optional(),
-});
-export type TextScrambleConfig = z.infer<typeof TextScrambleConfigSchema>;
-
-export const TypewriterAnimatorSchema = z.object({
- granularity: z.enum(["character", "word", "line"]).default("character"),
- cursor: TextCursorConfigSchema.default({
- enabled: true,
- style: "bar",
- customChar: "|",
- blinkFrequency: 2.0,
- hideOnComplete: false,
- width: 2.5,
- spacing: 3.0,
- }),
- cadence: TypingCadenceSchema.default({
- mode: "human",
- jitter: 0.35,
- commaPauseMultiplier: 3.0,
- sentencePauseMultiplier: 5.5,
- paragraphPauseMultiplier: 7.0,
- }),
- scramble: TextScrambleConfigSchema.default({
- enabled: false,
- charset: "alphanumeric",
- scrambleFramesPerChar: 8,
- highlightProbability: 0.25,
- }),
- soundSync: z.boolean().default(false),
-});
-export type TypewriterAnimator = z.infer<typeof TypewriterAnimatorSchema>;
-
-// ==============================================================================================
-// 3. AFTER EFFECTS PROCEDURAL SELECTORS
-// ==============================================================================================
-
-export const SelectorBasedOnSchema = z.enum([
- "characters",
- "characters_excluding_spaces",
- "words",
- "lines",
-]);
-export type SelectorBasedOn = z.infer<typeof SelectorBasedOnSchema>;
-
-export const SelectorUnitsSchema = z.enum(["percentage", "index"]);
-export type SelectorUnits = z.infer<typeof SelectorUnitsSchema>;
-
-export const SelectorShapeSchema = z.enum([
- "square",
- "ramp_up",
- "ramp_down",
- "triangle",
- "round",
- "smooth",
-]);
-export type SelectorShape = z.infer<typeof SelectorShapeSchema>;
-
-export const SelectorModeSchema = z.enum([
- "add",
- "subtract",
- "intersect",
- "min",
- "max",
- "difference",
-]);
-export type SelectorMode = z.infer<typeof SelectorModeSchema>;
-
-export const TextRangeSelectorSchema = z.object({
- type: z.literal("range"),
- start: z.number().default(0),
- end: z.number().default(1),
- offset: z.number().default(0),
- units: SelectorUnitsSchema.default("percentage"),
- basedOn: SelectorBasedOnSchema.default("characters"),
- shape: SelectorShapeSchema.default("square"),
- mode: SelectorModeSchema.default("add"),
- amount: z.number().min(-1).max(1).default(1),
- easeHigh: z.number().min(-1).max(1).default(0),
- easeLow: z.number().min(-1).max(1).default(0),
- randomizeOrder: z.boolean().default(false),
- randomSeed: z.number().int().default(12345),
-});
-export type TextRangeSelector = z.infer<typeof TextRangeSelectorSchema>;
-
-export const TextWigglySelectorSchema = z.object({
- type: z.literal("wiggly"),
- wigglesPerSecond: z.number().positive().default(2.0),
- correlation: z.number().min(0).max(1).default(0.5),
- temporalPhaseDeg: z.number().default(0),
- spatialPhaseDeg: z.number().default(0),
- minAmount: z.number().min(-1).max(1).default(-1.0),
- maxAmount: z.number().min(-1).max(1).default(1.0),
- randomSeed: z.number().int().default(42),
- basedOn: SelectorBasedOnSchema.default("characters"),
- mode: SelectorModeSchema.default("intersect"),
-});
-export type TextWigglySelector = z.infer<typeof TextWigglySelectorSchema>;
-
-export const TextSignalSelectorSchema = z.object({
- type: z.literal("signal"),
- signalId: z.string(),
- multiplier: z.number().default(1.0),
- offset: z.number().default(0.0),
- phaseSpread: z.number().default(0.0),
- basedOn: SelectorBasedOnSchema.default("characters"),
- mode: SelectorModeSchema.default("add"),
-});
-export type TextSignalSelector = z.infer<typeof TextSignalSelectorSchema>;
-
-export const TextSelectorSchema = z.discriminatedUnion("type", [
- TextRangeSelectorSchema,
- TextWigglySelectorSchema,
- TextSignalSelectorSchema,
-]);
-export type TextSelector = z.infer<typeof TextSelectorSchema>;
-
-// ==============================================================================================
-// 4. ANIMATABLE GLYPH PROPERTIES (TRANSFORM PAYLOAD)
-// ==============================================================================================
-
-export const AnimatableGlyphPropsSchema = z.object({
- x: z.number().optional(),
- y: z.number().optional(),
- z: z.number().optional(),
- scale: z.number().optional(),
- scaleX: z.number().optional(),
- scaleY: z.number().optional(),
- rotation: z.number().optional(),
- rotationX: z.number().optional(),
- rotationY: z.number().optional(),
- skew: z.number().optional(),
- skewAxis: z.number().optional(),
- tracking: z.number().optional(),
- lineSpacing: z.number().optional(),
- opacity: z.number().min(0).max(1).optional(),
- fillColor: z.string().optional(),
- strokeColor: z.string().optional(),
- strokeWidth: z.number().min(0).optional(),
- blur: z.number().min(0).optional(),
- characterOffset: z.number().int().optional(),
-});
-export type AnimatableGlyphProps = z.infer<typeof AnimatableGlyphPropsSchema>;
-
-export const GlyphAnchorGroupingSchema = z.enum([
- "character",
- "word",
- "line",
- "all",
-]);
-export type GlyphAnchorGrouping = z.infer<typeof GlyphAnchorGroupingSchema>;
-
-export const GlyphAnchorPointSchema = z.object({
- grouping: GlyphAnchorGroupingSchema.default("character"),
- anchorX: z.number().default(0.5),
- anchorY: z.number().default(0.8),
-});
-export type GlyphAnchorPoint = z.infer<typeof GlyphAnchorPointSchema>;
-
-export const AETextAnimatorSchema = z.object({
- name: z.string().default("Animator"),
- selectors: z.array(TextSelectorSchema).min(1),
- props: AnimatableGlyphPropsSchema,
- anchor: GlyphAnchorPointSchema.default({
- grouping: "character",
- anchorX: 0.5,
- anchorY: 0.8,
- }),
-});
-export type AETextAnimator = z.infer<typeof AETextAnimatorSchema>;
-
-export const EnhancedTextNodeSchema = z.object({
- text: z.string(),
- fontFamily: z.string().default("Inter"),
- fontSize: z.number().positive().default(24),
- fontWeight: z.union([z.number(), z.string()]).default(400),
- fill: z.string().default("#ffffff"),
- letterSpacing: z.number().default(0),
- lineHeight: z.number().positive().default(1.2),
- textAlign: z.enum(["left", "center", "right"]).default("left"),
- pathOptions: TextPathOptionsSchema.optional(),
- typewriter: TypewriterAnimatorSchema.optional(),
- animators: z.array(AETextAnimatorSchema).default([]),
-});
-export type EnhancedTextNode = z.infer<typeof EnhancedTextNodeSchema>;
diff --git a/packages/compositions/src/program/text-animations/typewriter.ts b/packages/compositions/src/program/text-animations/typewriter.ts
deleted file mode 100644
index 0e73a4bf..00000000
--- a/packages/compositions/src/program/text-animations/typewriter.ts
+++ /dev/null
@@ -1,166 +0,0 @@
-/**
- * @file packages/compositions/src/program/text-animations/typewriter.ts
- * Realistic human cadence typewriter simulation, matrix scramble cipher, and blinking cursor.
- */
-
-import type { TextScrambleConfig, TypingCadence } from "./types.js";
-
-const SCRAMBLE_POOLS: Record<string, string> = {
- ascii: "!@#$%^&*()_+{}[]:;<>,.?",
- alphanumeric: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
- matrix: "010101<>[]{}+=*~#%&!?/\\:;|X0123456789",
- binary: "01",
- hex: "0123456789ABCDEF",
-};
-
-export function getScramblePool(
- charset: string,
- customCharset?: string,
-): string {
- if (charset === "custom" && customCharset && customCharset.length > 0) {
- return customCharset;
- }
- return SCRAMBLE_POOLS[charset] ?? SCRAMBLE_POOLS.alphanumeric;
-}
-
-/**
- * Evaluates typewriter visible character count and scramble state for frame `currentFrame`.
- */
-export function evaluateTypewriterState(
- fullText: string,
- currentFrame: number,
- startFrame: number,
- durationFrames: number,
- cadence: TypingCadence = {
- mode: "human",
- jitter: 0.35,
- commaPauseMultiplier: 3.0,
- sentencePauseMultiplier: 5.5,
- paragraphPauseMultiplier: 7.0,
- },
- scramble: TextScrambleConfig = {
- enabled: false,
- charset: "alphanumeric",
- scrambleFramesPerChar: 8,
- highlightProbability: 0.25,
- },
- blinkFrequency = 2.0,
- granularity: "character" | "word" | "line" = "character",
- progressOverride?: number,
-): {
- visibleChars: number;
- currentDisplayString: string;
- isCursorVisible: boolean;
-} {
- if (progressOverride === undefined && currentFrame < startFrame) {
- return {
- visibleChars: 0,
- currentDisplayString: "",
- isCursorVisible: false,
- };
- }
-
- const progress =
- progressOverride !== undefined
- ? Math.max(0, Math.min(1, progressOverride))
- : Math.min(1, (currentFrame - startFrame) / Math.max(1, durationFrames));
- const elapsed =
- progressOverride !== undefined
- ? progress * durationFrames
- : currentFrame - startFrame;
- const totalChars = fullText.length;
-
- let visibleCount = Math.floor(progress * totalChars);
-
- if (granularity === "word") {
- const tokens = fullText.split(/(\s+)/);
- const wordIndices: number[] = [];
- tokens.forEach((t, i) => {
- if (t.trim().length > 0) wordIndices.push(i);
- });
- const visibleWords = Math.min(
- wordIndices.length,
- Math.floor(progress * (wordIndices.length + 1)),
- );
- if (visibleWords <= 0) {
- visibleCount = 0;
- } else {
- const lastIdx =
- wordIndices[Math.min(wordIndices.length - 1, visibleWords - 1)];
- visibleCount = tokens.slice(0, lastIdx + 1).join("").length;
- }
- } else if (granularity === "line") {
- const lines = fullText.split("\n");
- const visibleLines = Math.min(
- lines.length,
- Math.floor(progress * (lines.length + 1)),
- );
- visibleCount = lines.slice(0, visibleLines).join("\n").length;
- } else if (cadence.mode === "human" && progress < 1) {
- // Apply punctuation cadence delays if human mode is enabled
- let simulatedCharIndex = 0;
- let simulatedElapsed = 0;
- const baseFramesPerChar = durationFrames / Math.max(1, totalChars);
-
- for (let i = 0; i < totalChars; i++) {
- const char = fullText[i];
- let multiplier = 1.0;
- const commaMult = cadence.commaPauseMultiplier ?? 3.0;
- const sentenceMult = cadence.sentencePauseMultiplier ?? 5.5;
- const paraMult = cadence.paragraphPauseMultiplier ?? 7.0;
- const jitter = cadence.jitter ?? 0.35;
-
- if (char === "," || char === ";") multiplier = commaMult;
- else if (char === "." || char === "!" || char === "?")
- multiplier = sentenceMult;
- else if (char === "\n") multiplier = paraMult;
-
- // Add small deterministic jitter
- const jitterVal = 1 + Math.sin(i * 17.3 + 42) * 0.5 * jitter;
- const charDuration = baseFramesPerChar * multiplier * jitterVal;
-
- simulatedElapsed += charDuration;
- if (simulatedElapsed <= elapsed) {
- simulatedCharIndex = i + 1;
- } else {
- break;
- }
- }
- visibleCount = Math.min(totalChars, simulatedCharIndex);
- }
-
- let displayStr = fullText.slice(0, visibleCount);
-
- // Scramble pool substitution on the trailing edge of typed characters
- if (scramble.enabled && visibleCount < totalChars && visibleCount > 0) {
- const pool = getScramblePool(scramble.charset, scramble.customCharset);
- const trailCount = Math.min(
- visibleCount,
- Math.max(1, scramble.scrambleFramesPerChar > 4 ? 3 : 2),
- );
- let scrambledTail = "";
- for (let t = 0; t < trailCount; t++) {
- const idx =
- Math.floor(
- Math.abs(Math.sin(currentFrame * 7 + t * 13)) * pool.length,
- ) % pool.length;
- scrambledTail += pool[idx];
- }
- displayStr =
- displayStr.slice(0, displayStr.length - trailCount) + scrambledTail;
- }
-
- // Cursor blink based on frequency (Hz)
- // At 30fps and 2Hz -> 15 frames per cycle (7.5 on, 7.5 off)
- let isCursorVisible = true;
- if (blinkFrequency > 0) {
- const halfPeriodFrames = Math.max(1, Math.round(30 / (2 * blinkFrequency)));
- isCursorVisible = Math.floor(elapsed / halfPeriodFrames) % 2 === 0;
- }
-
- return {
- visibleChars: visibleCount,
- currentDisplayString: displayStr,
- isCursorVisible,
- };
-}
diff --git a/packages/core/src/signals/index.ts b/packages/core/src/signals/index.ts
index 4b394012..dce89e78 100644
--- a/packages/core/src/signals/index.ts
+++ b/packages/core/src/signals/index.ts
@@ -487,8 +487,9 @@ export interface SineSignalOptions {
phase?: number;
}
-export function sineSignal(
- options: SineSignalOptions = {},
+function periodicSignal(
+ wave: (angle: number) => number,
+ options: SineSignalOptions,
): FrameSignal<number> {
const min = options.min;
const max = options.max;
@@ -518,45 +519,21 @@ export function sineSignal(
const freq = frequency ?? 1.0;
angle = 2 * Math.PI * freq * ctx.time + phase;
}
- return offset + amplitude * Math.sin(angle);
+ return offset + amplitude * wave(angle);
},
});
}
-export function cosineSignal(
+export function sineSignal(
options: SineSignalOptions = {},
): FrameSignal<number> {
- const min = options.min;
- const max = options.max;
- let amplitude = options.amplitude ?? 1.0;
- let offset = options.offset ?? 0.0;
-
- if (min !== undefined && max !== undefined) {
- offset = (min + max) / 2;
- amplitude = (max - min) / 2;
- } else if (min !== undefined) {
- offset = min + amplitude;
- } else if (max !== undefined) {
- offset = max - amplitude;
- }
-
- const period = options.period;
- const frequency = options.frequency;
- const phase = options.phase ?? 0.0;
+ return periodicSignal(Math.sin, options);
+}
- return signalBuilder({
- type: "custom",
- fn: (ctx: FrameContext) => {
- let angle: number;
- if (period !== undefined && period > 0) {
- angle = (2 * Math.PI * ctx.frame) / period + phase;
- } else {
- const freq = frequency ?? 1.0;
- angle = 2 * Math.PI * freq * ctx.time + phase;
- }
- return offset + amplitude * Math.cos(angle);
- },
- });
+export function cosineSignal(
+ options: SineSignalOptions = {},
+): FrameSignal<number> {
+ return periodicSignal(Math.cos, options);
}
export {
diff --git a/packages/framefields/src/audio-mesh-render.test.ts b/packages/framefields/src/audio-mesh-render.test.ts
index 50422009..7c0db680 100644
--- a/packages/framefields/src/audio-mesh-render.test.ts
+++ b/packages/framefields/src/audio-mesh-render.test.ts
@@ -2,47 +2,9 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { Signal } from "@framefields/core";
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import { Composition, Layer, Layer3D, Light } from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels } from "./test-utils/pixels.js";
// 3D Cylinder / Column mesh with multiple height slices for wave and normal displacement
function generateCylinderObj(
@@ -233,7 +195,9 @@ describe("Headless WebGPU Audio Latent 3D Mesh Deformation Engine", () => {
});
it("renders harmonic_wave deformation modulated reactively by Signal multiplier", async () => {
- const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "framefields-wave-"));
+ const tmpDir = await fs.mkdtemp(
+ path.join(os.tmpdir(), "framefields-wave-"),
+ );
const objPath = path.join(tmpDir, "cylinder_wave.obj");
await fs.writeFile(objPath, generateCylinderObj(16, 8, 60, 220));
@@ -319,16 +283,46 @@ describe("Headless WebGPU Audio Latent 3D Mesh Deformation Engine", () => {
backgroundColor: "#080c18",
});
comp.add(
- Layer.camera({ id: "cam-bar", x: 320, y: 240, z: -500, targetX: 320, targetY: 240, targetZ: 0 }),
+ Layer.camera({
+ id: "cam-bar",
+ x: 320,
+ y: 240,
+ z: -500,
+ targetX: 320,
+ targetY: 240,
+ targetZ: 0,
+ }),
);
comp.add(Light.ambient("#ffffff", 0.5));
comp.add(
- Light.directional({ id: "sun", color: "#ffffff", intensity: 1.2, x: 100, y: -300, z: -400, targetX: 320, targetY: 240, targetZ: 0 }),
+ Light.directional({
+ id: "sun",
+ color: "#ffffff",
+ intensity: 1.2,
+ x: 100,
+ y: -300,
+ z: -400,
+ targetX: 320,
+ targetY: 240,
+ targetZ: 0,
+ }),
);
- const bar = Layer3D.obj(objPath, { id: "bar", x: 320, y: 240, z: 0, color: "#f0f0f0", material: "lit" });
+ const bar = Layer3D.obj(objPath, {
+ id: "bar",
+ x: 320,
+ y: 240,
+ z: 0,
+ color: "#f0f0f0",
+ material: "lit",
+ });
comp.add(
mode
- ? bar.deformWithAudio({ audioTrackId: `bar-${mode}`, audioData: [audioChannel], mode, amplitudeMultiplier: 1.5 })
+ ? bar.deformWithAudio({
+ audioTrackId: `bar-${mode}`,
+ audioData: [audioChannel],
+ mode,
+ amplitudeMultiplier: 1.5,
+ })
: bar,
);
const img = await decodePngPixels(await comp.renderFrame({ frame }));
diff --git a/packages/framefields/src/camera-3d-render.test.ts b/packages/framefields/src/camera-3d-render.test.ts
index ad742097..d9be2c48 100644
--- a/packages/framefields/src/camera-3d-render.test.ts
+++ b/packages/framefields/src/camera-3d-render.test.ts
@@ -1,63 +1,6 @@
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import { CameraAnimation, Composition, Layer } from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels, getPixel } from "./test-utils/pixels.js";
describe("WebGPU 3D Camera & Multiplane Render Conformance", () => {
it("renders 3D camera dolly with multiplane depth layers and optical scaling", async () => {
diff --git a/packages/framefields/src/effect-signals-composition-frame.test.ts b/packages/framefields/src/effect-signals-composition-frame.test.ts
index 994eec3d..4e85c49a 100644
--- a/packages/framefields/src/effect-signals-composition-frame.test.ts
+++ b/packages/framefields/src/effect-signals-composition-frame.test.ts
@@ -1,21 +1,8 @@
import { existsSync } from "node:fs";
import { resolve } from "node:path";
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import { Blur, Composition, Layer, Signal, Vignette } from "./index.js";
-
-async function decodePngPixels(pngBuffer: Buffer) {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
+import { decodePngPixels } from "./test-utils/pixels.js";
const testVideoPath = resolve(
import.meta.dirname,
diff --git a/packages/framefields/src/effects/golden.test.ts b/packages/framefields/src/effects/golden.test.ts
index 6c24de35..c6774d84 100644
--- a/packages/framefields/src/effects/golden.test.ts
+++ b/packages/framefields/src/effects/golden.test.ts
@@ -4,11 +4,10 @@ import {
Blur,
ColorBalance,
type Effect,
- Layer,
type ObjectTrackSignals,
Vignette,
- VisionNode,
} from "../index.js";
+import { attachSyntheticPersonTrack } from "../test-utils/vision.js";
/**
* Golden snapshots of what each effect class hands the renderer.
@@ -50,38 +49,7 @@ function describeEffect(effect: Effect<object>) {
}
function syntheticTrack(): ObjectTrackSignals {
- const video = Layer.video("test.mp4");
- const vision = VisionNode.attach(video, {
- enableDetection: true,
- classes: ["person"],
- });
- vision.setObjectResult(0, [
- {
- trackId: 1,
- category: "person",
- score: 0.95,
- boundingBox: {
- originX: 400,
- originY: 300,
- width: 200,
- height: 500,
- normalizedX: 400 / 1920,
- normalizedY: 300 / 1080,
- normalizedWidth: 200 / 1920,
- normalizedHeight: 500 / 1080,
- },
- centerX: 500,
- centerY: 550,
- normalizedCenterX: 500 / 1920,
- normalizedCenterY: 550 / 1080,
- velocity: { vx: 0, vy: 0 },
- speed: 0,
- age: 1,
- hits: 1,
- active: true,
- isCoasting: false,
- },
- ]);
+ const { vision } = attachSyntheticPersonTrack();
return vision.objects.byCategory("person");
}
diff --git a/packages/framefields/src/extended-models-render.test.ts b/packages/framefields/src/extended-models-render.test.ts
index c7c1ce90..9292a3d8 100644
--- a/packages/framefields/src/extended-models-render.test.ts
+++ b/packages/framefields/src/extended-models-render.test.ts
@@ -2,66 +2,9 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { Signal } from "@framefields/core";
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import { Composition, Layer, Layer3D, LayerAnimation, Light } from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels, getPixel } from "./test-utils/pixels.js";
describe("Extended WebGPU 3D Model Rendering Conformance (glTF/GLB, STL, PLY, VOX, 3DS, OFF)", () => {
it("renders binary GLB 3D models with directional lighting and signal reactivity", async () => {
diff --git a/packages/framefields/src/lights-3d-render.test.ts b/packages/framefields/src/lights-3d-render.test.ts
index c4213b54..5524460d 100644
--- a/packages/framefields/src/lights-3d-render.test.ts
+++ b/packages/framefields/src/lights-3d-render.test.ts
@@ -1,63 +1,6 @@
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import { Composition, Layer, LayerAnimation } from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels, getPixel } from "./test-utils/pixels.js";
describe("WebGPU 3D Lighting & Shading Conformance", () => {
it("renders unlit mode when no lights are present (backward compatibility)", async () => {
diff --git a/packages/framefields/src/model3d-render.test.ts b/packages/framefields/src/model3d-render.test.ts
index 3a64afdb..9c0bb1a0 100644
--- a/packages/framefields/src/model3d-render.test.ts
+++ b/packages/framefields/src/model3d-render.test.ts
@@ -2,66 +2,9 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { Signal } from "@framefields/core";
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import { Composition, Layer, Layer3D, LayerAnimation, Light } from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels, getPixel } from "./test-utils/pixels.js";
const SAMPLE_OBJ = `
# Wavefront OBJ 3D Pyramid
diff --git a/packages/framefields/src/module4-effects-signals.test.ts b/packages/framefields/src/module4-effects-signals.test.ts
index 1ce208ca..7b776b44 100644
--- a/packages/framefields/src/module4-effects-signals.test.ts
+++ b/packages/framefields/src/module4-effects-signals.test.ts
@@ -1,4 +1,3 @@
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import {
Composition,
@@ -8,63 +7,7 @@ import {
Signal,
TemporalDeflicker,
} from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels, getPixel } from "./test-utils/pixels.js";
describe("Module 4: Signal-Based & Effects-Based Pipeline Conformance", () => {
it("1. TemporalDeflicker Effect Integration: layer.withEffects(Effect.deflicker()) compiles and renders headlessly", async () => {
diff --git a/packages/framefields/src/render-conformance.test.ts b/packages/framefields/src/render-conformance.test.ts
index 8bf958c8..b3a8d1f0 100644
--- a/packages/framefields/src/render-conformance.test.ts
+++ b/packages/framefields/src/render-conformance.test.ts
@@ -1,5 +1,4 @@
import type { VirtualMediaData } from "@framefields/core";
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import {
Composition,
@@ -10,68 +9,18 @@ import {
TextPathBuilder,
} from "./index.js";
import { HeadlessMediaRenderer } from "./renderer/index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
+import {
+ computeMse,
+ type DecodedImage,
+ decodePngPixels,
+ getPixel,
+ type PixelRgba,
+} from "./test-utils/pixels.js";
function getLuminance(pixel: PixelRgba): number {
return 0.299 * pixel.r + 0.587 * pixel.g + 0.114 * pixel.b;
}
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
-
describe("Rendering Conformance Test Suite (Headless WebGPU & Pixel Verification)", () => {
const renderer = new HeadlessMediaRenderer();
diff --git a/packages/framefields/src/screen-space-relight.test.ts b/packages/framefields/src/screen-space-relight.test.ts
index de96b7fa..55c4ab70 100644
--- a/packages/framefields/src/screen-space-relight.test.ts
+++ b/packages/framefields/src/screen-space-relight.test.ts
@@ -1,63 +1,6 @@
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import { Composition, Layer, LayerAnimation, Light } from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels, getPixel } from "./test-utils/pixels.js";
describe("Screen-Space 3D Normal Relighting Conformance", () => {
it("1. Flat Fallback Invariant: Layer with relighting renders gracefully when no lights are present", async () => {
diff --git a/packages/framefields/src/section-effects.test.ts b/packages/framefields/src/section-effects.test.ts
index c0401153..450d1a2d 100644
--- a/packages/framefields/src/section-effects.test.ts
+++ b/packages/framefields/src/section-effects.test.ts
@@ -9,6 +9,7 @@ import {
Vignette,
VisionNode,
} from "./index.js";
+import { attachSyntheticPersonTrack } from "./test-utils/vision.js";
describe("Unified Composition & Layer Section Architecture Suite", () => {
function setupVisionWithSyntheticTrack(): {
@@ -16,40 +17,7 @@ describe("Unified Composition & Layer Section Architecture Suite", () => {
vision: ReturnType<typeof VisionNode.attach>;
targetPerson: ObjectTrackSignals;
} {
- const video = Layer.video("test_source.mp4");
- const vision = VisionNode.attach(video, {
- enableDetection: true,
- classes: ["person"],
- });
-
- // Populate synthetic bounding box at frame 0: origin (400, 300), size (200, 500)
- vision.setObjectResult(0, [
- {
- trackId: 1,
- category: "person",
- score: 0.95,
- boundingBox: {
- originX: 400,
- originY: 300,
- width: 200,
- height: 500,
- normalizedX: 400 / 1920,
- normalizedY: 300 / 1080,
- normalizedWidth: 200 / 1920,
- normalizedHeight: 500 / 1080,
- },
- centerX: 500,
- centerY: 550,
- normalizedCenterX: 500 / 1920,
- normalizedCenterY: 550 / 1080,
- velocity: { vx: 0, vy: 0 },
- speed: 0,
- age: 1,
- hits: 1,
- active: true,
- isCoasting: false,
- },
- ]);
+ const { video, vision } = attachSyntheticPersonTrack("test_source.mp4");
const targetPerson = vision.objects.byCategory("person");
return { video, vision, targetPerson };
diff --git a/packages/framefields/src/shapes-3d-render.test.ts b/packages/framefields/src/shapes-3d-render.test.ts
index 6eea2c7d..8e043a8a 100644
--- a/packages/framefields/src/shapes-3d-render.test.ts
+++ b/packages/framefields/src/shapes-3d-render.test.ts
@@ -1,4 +1,3 @@
-import { Canvas, loadImage } from "skia-canvas";
import { describe, expect, it } from "vitest";
import {
Composition,
@@ -7,63 +6,7 @@ import {
LayerAnimation,
Path3D,
} from "./index.js";
-
-interface DecodedImage {
- width: number;
- height: number;
- pixels: Uint8ClampedArray;
-}
-
-interface PixelRgba {
- r: number;
- g: number;
- b: number;
- a: number;
-}
-
-async function decodePngPixels(pngBuffer: Buffer): Promise<DecodedImage> {
- const img = await loadImage(pngBuffer);
- const canvas = new Canvas(img.width, img.height);
- const ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0);
- const imgData = ctx.getImageData(0, 0, img.width, img.height);
- return {
- width: img.width,
- height: img.height,
- pixels: imgData.data,
- };
-}
-
-function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
- const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
- const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
- const index = (clampedY * image.width + clampedX) * 4;
- return {
- r: image.pixels[index] ?? 0,
- g: image.pixels[index + 1] ?? 0,
- b: image.pixels[index + 2] ?? 0,
- a: image.pixels[index + 3] ?? 0,
- };
-}
-
-function computeMse(
- pixelsA: Uint8ClampedArray,
- pixelsB: Uint8ClampedArray,
-): number {
- if (pixelsA.length !== pixelsB.length) {
- throw new Error(
- `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
- );
- }
- let sumSquaredDiff = 0;
- for (let i = 0; i < pixelsA.length; i += 4) {
- const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
- const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
- const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
- sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
- }
- return sumSquaredDiff / (pixelsA.length / 4);
-}
+import { computeMse, decodePngPixels, getPixel } from "./test-utils/pixels.js";
describe("WebGPU 3D Shapes & Path Follower Headless Conformance", () => {
it("renders a 3D volumetric cube headlessly with true depth and orientation", async () => {
diff --git a/packages/framefields/src/test-utils/pixels.ts b/packages/framefields/src/test-utils/pixels.ts
new file mode 100644
index 00000000..3166dcf9
--- /dev/null
+++ b/packages/framefields/src/test-utils/pixels.ts
@@ -0,0 +1,61 @@
+// Pixel helpers for render tests (decoding PNG frames and comparing them).
+import { Canvas, loadImage } from "skia-canvas";
+
+export interface DecodedImage {
+ width: number;
+ height: number;
+ pixels: Uint8ClampedArray;
+}
+
+export interface PixelRgba {
+ r: number;
+ g: number;
+ b: number;
+ a: number;
+}
+
+export async function decodePngPixels(
+ pngBuffer: Buffer,
+): Promise<DecodedImage> {
+ const img = await loadImage(pngBuffer);
+ const canvas = new Canvas(img.width, img.height);
+ const ctx = canvas.getContext("2d");
+ ctx.drawImage(img, 0, 0);
+ const imgData = ctx.getImageData(0, 0, img.width, img.height);
+ return {
+ width: img.width,
+ height: img.height,
+ pixels: imgData.data,
+ };
+}
+
+export function getPixel(image: DecodedImage, x: number, y: number): PixelRgba {
+ const clampedX = Math.max(0, Math.min(image.width - 1, Math.round(x)));
+ const clampedY = Math.max(0, Math.min(image.height - 1, Math.round(y)));
+ const index = (clampedY * image.width + clampedX) * 4;
+ return {
+ r: image.pixels[index] ?? 0,
+ g: image.pixels[index + 1] ?? 0,
+ b: image.pixels[index + 2] ?? 0,
+ a: image.pixels[index + 3] ?? 0,
+ };
+}
+
+export function computeMse(
+ pixelsA: Uint8ClampedArray,
+ pixelsB: Uint8ClampedArray,
+): number {
+ if (pixelsA.length !== pixelsB.length) {
+ throw new Error(
+ `Pixel buffer size mismatch: ${pixelsA.length} vs ${pixelsB.length}`,
+ );
+ }
+ let sumSquaredDiff = 0;
+ for (let i = 0; i < pixelsA.length; i += 4) {
+ const dr = (pixelsA[i] ?? 0) - (pixelsB[i] ?? 0);
+ const dg = (pixelsA[i + 1] ?? 0) - (pixelsB[i + 1] ?? 0);
+ const db = (pixelsA[i + 2] ?? 0) - (pixelsB[i + 2] ?? 0);
+ sumSquaredDiff += (dr * dr + dg * dg + db * db) / 3;
+ }
+ return sumSquaredDiff / (pixelsA.length / 4);
+}
diff --git a/packages/framefields/src/test-utils/vision.ts b/packages/framefields/src/test-utils/vision.ts
new file mode 100644
index 00000000..9ed9f234
--- /dev/null
+++ b/packages/framefields/src/test-utils/vision.ts
@@ -0,0 +1,41 @@
+import { Layer, VisionNode } from "../index.js";
+
+/**
+ * A video with person detection whose frame-0 result is one synthetic person:
+ * a 200×500 box at (400, 300) in a 1920×1080 frame.
+ */
+export function attachSyntheticPersonTrack(src = "test.mp4") {
+ const video = Layer.video(src);
+ const vision = VisionNode.attach(video, {
+ enableDetection: true,
+ classes: ["person"],
+ });
+ vision.setObjectResult(0, [
+ {
+ trackId: 1,
+ category: "person",
+ score: 0.95,
+ boundingBox: {
+ originX: 400,
+ originY: 300,
+ width: 200,
+ height: 500,
+ normalizedX: 400 / 1920,
+ normalizedY: 300 / 1080,
+ normalizedWidth: 200 / 1920,
+ normalizedHeight: 500 / 1080,
+ },
+ centerX: 500,
+ centerY: 550,
+ normalizedCenterX: 500 / 1920,
+ normalizedCenterY: 550 / 1080,
+ velocity: { vx: 0, vy: 0 },
+ speed: 0,
+ age: 1,
+ hits: 1,
+ active: true,
+ isCoasting: false,
+ },
+ ]);
+ return { video, vision };
+}
diff --git a/packages/framefields/src/tracked-region-blur.test.ts b/packages/framefields/src/tracked-region-blur.test.ts
index f36449a4..f1c16e51 100644
--- a/packages/framefields/src/tracked-region-blur.test.ts
+++ b/packages/framefields/src/tracked-region-blur.test.ts
@@ -2,51 +2,19 @@ import { describe, expect, it } from "vitest";
import {
Blur,
Composition,
- Effect,
type EditableObjectTrack,
+ Effect,
Layer,
VisionNode,
} from "./index.js";
+import { attachSyntheticPersonTrack } from "./test-utils/vision.js";
describe("Tracked Section Editing & Blur Engine Suite", () => {
function setupVisionWithSyntheticTrack(): {
vision: ReturnType<typeof VisionNode.attach>;
targetPerson: EditableObjectTrack;
} {
- const video = Layer.video("test.mp4");
- const vision = VisionNode.attach(video, {
- enableDetection: true,
- classes: ["person"],
- });
-
- // Populate synthetic bounding box at frame 0: origin (400, 300), size (200, 500)
- vision.setObjectResult(0, [
- {
- trackId: 1,
- category: "person",
- score: 0.95,
- boundingBox: {
- originX: 400,
- originY: 300,
- width: 200,
- height: 500,
- normalizedX: 400 / 1920,
- normalizedY: 300 / 1080,
- normalizedWidth: 200 / 1920,
- normalizedHeight: 500 / 1080,
- },
- centerX: 500,
- centerY: 550,
- normalizedCenterX: 500 / 1920,
- normalizedCenterY: 550 / 1080,
- velocity: { vx: 0, vy: 0 },
- speed: 0,
- age: 1,
- hits: 1,
- active: true,
- isCoasting: false,
- },
- ]);
+ const { vision } = attachSyntheticPersonTrack();
// Vision decorates every track with its editing helpers.
const targetPerson = vision.objects.byCategory(
diff --git a/packages/media/server/utils/index.ts b/packages/media/server/utils/index.ts
index f9fac6aa..ae85f61a 100644
--- a/packages/media/server/utils/index.ts
+++ b/packages/media/server/utils/index.ts
@@ -7,6 +7,81 @@ import { mediaLogger } from "@framefields/server-utils";
import { fileTypeFromBuffer } from "file-type";
import sharp from "sharp";
+interface ProbeInput {
+ inputPath: string;
+ /** File type detected from a buffer input. */
+ type?: { ext: string; mime: string };
+ /** Deletes the temp file a buffer input was written to. */
+ cleanup: (logMessage?: string) => Promise<void>;
+}
+
+/**
+ * A path ffprobe can read: URLs as-is, buffers written to a temp file named
+ * `${prefix}_<id><ext>` (ext from the detected type, else `fallbackExt`).
+ */
+async function prepareProbeInput(
+ bufferOrUrl: Buffer | string,
+ prefix: string,
+ fallbackExt?: (type: { ext: string; mime: string } | undefined) => string,
+): Promise<ProbeInput> {
+ if (typeof bufferOrUrl === "string") {
+ return { inputPath: bufferOrUrl, cleanup: async () => {} };
+ }
+ const type = await fileTypeFromBuffer(bufferOrUrl);
+ const ext = type ? `.${type.ext}` : (fallbackExt?.(type) ?? "");
+ const tempFile = path.join(os.tmpdir(), `${prefix}_${generateId()}${ext}`);
+ await fs.writeFile(tempFile, bufferOrUrl);
+ return {
+ inputPath: tempFile,
+ type,
+ cleanup: async (logMessage = "Failed to delete temp file") => {
+ try {
+ await fs.unlink(tempFile);
+ } catch (err) {
+ mediaLogger.error({ err }, logMessage);
+ }
+ },
+ };
+}
+
+/** Runs ffprobe to completion; rejects only when it can't be started. */
+function runFfprobe(
+ args: string[],
+): Promise<{ code: number | null; stdout: string; stderr: string }> {
+ return new Promise((resolve, reject) => {
+ const ffprobe = spawn("ffprobe", args);
+ let stdout = "";
+ let stderr = "";
+ ffprobe.stdout.on("data", (data) => {
+ stdout += data.toString();
+ });
+ ffprobe.stderr.on("data", (data) => {
+ stderr += data.toString();
+ });
+ ffprobe.on("error", (err) => {
+ reject(new Error(`Failed to start ffprobe: ${err.message}`));
+ });
+ ffprobe.on("close", (code) => {
+ resolve({ code, stdout, stderr });
+ });
+ });
+}
+
+const MIME_EXTENSIONS: Record<string, string> = {
+ "audio/mpeg": ".mp3",
+ "audio/mp3": ".mp3",
+ "audio/wav": ".wav",
+ "audio/x-wav": ".wav",
+ "audio/ogg": ".ogg",
+ "audio/aac": ".aac",
+ "audio/m4a": ".m4a",
+ "audio/x-m4a": ".m4a",
+ "audio/mp4": ".m4a",
+ "video/mp4": ".mp4",
+ "video/quicktime": ".mov",
+ "video/x-matroska": ".mkv",
+};
+
/**
* Gets the duration of media (video/audio) using ffprobe.
*/
@@ -14,96 +89,38 @@ export async function getMediaDuration(
bufferOrUrl: Buffer | string,
mimeType?: string,
): Promise<number | null> {
- const isUrl = typeof bufferOrUrl === "string";
- let inputPath = "";
- let tempFile = "";
- let type: { ext: string; mime: string } | undefined;
-
- if (isUrl) {
- inputPath = bufferOrUrl;
- } else {
- const tempDir = os.tmpdir();
- type = await fileTypeFromBuffer(bufferOrUrl);
-
- // Try to get extension from detected type, otherwise fallback to mimeType hint
- let ext = type ? `.${type.ext}` : "";
- if (!ext && mimeType) {
- const mimeMap: Record<string, string> = {
- "audio/mpeg": ".mp3",
- "audio/mp3": ".mp3",
- "audio/wav": ".wav",
- "audio/x-wav": ".wav",
- "audio/ogg": ".ogg",
- "audio/aac": ".aac",
- "audio/m4a": ".m4a",
- "audio/x-m4a": ".m4a",
- "audio/mp4": ".m4a",
- "video/mp4": ".mp4",
- "video/quicktime": ".mov",
- "video/x-matroska": ".mkv",
- };
- ext = mimeMap[mimeType] || "";
- }
-
- tempFile = path.join(tempDir, `temp_media_${generateId()}${ext}`);
- await fs.writeFile(tempFile, bufferOrUrl);
- inputPath = tempFile;
- }
+ // Extension from the detected type, otherwise from the mimeType hint
+ const { inputPath, type, cleanup } = await prepareProbeInput(
+ bufferOrUrl,
+ "temp_media",
+ () => (mimeType && MIME_EXTENSIONS[mimeType]) || "",
+ );
try {
- return await new Promise((resolve, reject) => {
- const ffprobe = spawn("ffprobe", [
- "-v",
- "error",
- "-show_entries",
- "format=duration",
- "-of",
- "default=noprint_wrappers=1:nokey=1",
- inputPath,
- ]);
-
- let output = "";
- let errorOutput = "";
-
- ffprobe.stdout.on("data", (data) => {
- output += data.toString();
- });
-
- ffprobe.stderr.on("data", (data) => {
- errorOutput += data.toString();
- });
-
- ffprobe.on("error", (err) => {
- reject(new Error(`Failed to start ffprobe: ${err.message}`));
- });
-
- ffprobe.on("close", (code) => {
- if (code === 0) {
- const duration = parseFloat(output.trim());
- resolve(Number.isNaN(duration) ? null : duration);
- } else {
- const details = isUrl
- ? `url: ${bufferOrUrl}`
- : `buffer size: ${bufferOrUrl.length}, hint: ${mimeType}, detected: ${type?.mime}`;
- reject(
- new Error(
- `ffprobe exited with code ${code}. stderr: ${errorOutput.trim()}. details: ${details}`,
- ),
- );
- }
- });
- });
- } finally {
- if (tempFile) {
- try {
- await fs.unlink(tempFile);
- } catch (err) {
- mediaLogger.error({ err }, "Failed to delete temp file");
- }
+ const { code, stdout, stderr } = await runFfprobe([
+ "-v",
+ "error",
+ "-show_entries",
+ "format=duration",
+ "-of",
+ "default=noprint_wrappers=1:nokey=1",
+ inputPath,
+ ]);
+ if (code !== 0) {
+ const details =
+ typeof bufferOrUrl === "string"
+ ? `url: ${bufferOrUrl}`
+ : `buffer size: ${bufferOrUrl.length}, hint: ${mimeType}, detected: ${type?.mime}`;
+ throw new Error(
+ `ffprobe exited with code ${code}. stderr: ${stderr.trim()}. details: ${details}`,
+ );
}
+ const duration = parseFloat(stdout.trim());
+ return Number.isNaN(duration) ? null : duration;
+ } finally {
+ await cleanup();
}
}
-
/**
* Gets video metadata (width, height, fps, duration) using ffprobe.
*/
@@ -113,110 +130,70 @@ export async function getVideoMetadata(bufferOrUrl: Buffer | string): Promise<{
fps: number;
duration: number;
} | null> {
- const isUrl = typeof bufferOrUrl === "string";
- let inputPath = "";
- let tempFile = "";
-
- if (isUrl) {
- inputPath = bufferOrUrl;
- } else {
- const tempDir = os.tmpdir();
- const type = await fileTypeFromBuffer(bufferOrUrl);
- const ext = type ? `.${type.ext}` : "";
- tempFile = path.join(tempDir, `temp_video_meta_${generateId()}${ext}`);
- await fs.writeFile(tempFile, bufferOrUrl);
- inputPath = tempFile;
- }
+ const { inputPath, cleanup } = await prepareProbeInput(
+ bufferOrUrl,
+ "temp_video_meta",
+ );
try {
- return await new Promise((resolve, reject) => {
- const ffprobe = spawn("ffprobe", [
- "-v",
- "error",
- "-select_streams",
- "v:0",
- "-show_entries",
- "stream=width,height,avg_frame_rate,duration",
- "-of",
- "json",
- inputPath,
- ]);
-
- let output = "";
- let errorOutput = "";
-
- ffprobe.stdout.on("data", (data) => {
- output += data.toString();
- });
-
- ffprobe.stderr.on("data", (data) => {
- errorOutput += data.toString();
- });
-
- ffprobe.on("error", (err) => {
- reject(new Error(`Failed to start ffprobe: ${err.message}`));
- });
-
- ffprobe.on("close", (code) => {
- if (code === 0) {
- try {
- const data = JSON.parse(output);
- const stream = data.streams?.[0];
-
- if (!stream) {
- return resolve(null);
- }
-
- const width = Number(stream.width);
- const height = Number(stream.height);
- const duration = parseFloat(stream.duration);
-
- // Parse avg_frame_rate (e.g., "30/1" or "24000/1001").
- // Chromium MediaRecorder WebM tracks often lack a
- // DefaultDuration element; ffprobe then reports 1000/1
- // (a ~1ms-per-timestamp fallback) even though the capture
- // is real 30/60fps. Guard against absurd values.
- let fps = 0;
- if (stream.avg_frame_rate) {
- const [num, den] = stream.avg_frame_rate.split("/");
- if (num && den) {
- fps = Number(num) / Number(den);
- }
- }
- // Never trust frame rates above 120fps or below 1fps from
- // container heuristics — treat them as unknown so we fall
- // back to a real measurement path.
- if (Number.isFinite(fps) && (fps < 1 || fps > 120)) fps = 0;
-
- resolve({
- width: Number.isNaN(width) ? 0 : width,
- height: Number.isNaN(height) ? 0 : height,
- fps: Number.isNaN(fps) ? 0 : fps,
- duration: Number.isNaN(duration) ? 0 : duration,
- });
- } catch (err) {
- reject(new Error(`Failed to parse ffprobe output: ${err}`));
- }
- } else {
- reject(
- new Error(
- `ffprobe exited with code ${code}. stderr: ${errorOutput.trim()}`,
- ),
- );
+ const { code, stdout, stderr } = await runFfprobe([
+ "-v",
+ "error",
+ "-select_streams",
+ "v:0",
+ "-show_entries",
+ "stream=width,height,avg_frame_rate,duration",
+ "-of",
+ "json",
+ inputPath,
+ ]);
+ if (code !== 0) {
+ throw new Error(
+ `ffprobe exited with code ${code}. stderr: ${stderr.trim()}`,
+ );
+ }
+ try {
+ const data = JSON.parse(stdout);
+ const stream = data.streams?.[0];
+
+ if (!stream) {
+ return null;
+ }
+
+ const width = Number(stream.width);
+ const height = Number(stream.height);
+ const duration = parseFloat(stream.duration);
+
+ // Parse avg_frame_rate (e.g., "30/1" or "24000/1001").
+ // Chromium MediaRecorder WebM tracks often lack a
+ // DefaultDuration element; ffprobe then reports 1000/1
+ // (a ~1ms-per-timestamp fallback) even though the capture
+ // is real 30/60fps. Guard against absurd values.
+ let fps = 0;
+ if (stream.avg_frame_rate) {
+ const [num, den] = stream.avg_frame_rate.split("/");
+ if (num && den) {
+ fps = Number(num) / Number(den);
}
- });
- });
- } finally {
- if (tempFile) {
- try {
- await fs.unlink(tempFile);
- } catch (err) {
- mediaLogger.error({ err }, "Failed to delete temp file");
}
+ // Never trust frame rates above 120fps or below 1fps from
+ // container heuristics — treat them as unknown so we fall
+ // back to a real measurement path.
+ if (Number.isFinite(fps) && (fps < 1 || fps > 120)) fps = 0;
+
+ return {
+ width: Number.isNaN(width) ? 0 : width,
+ height: Number.isNaN(height) ? 0 : height,
+ fps: Number.isNaN(fps) ? 0 : fps,
+ duration: Number.isNaN(duration) ? 0 : duration,
+ };
+ } catch (err) {
+ throw new Error(`Failed to parse ffprobe output: ${err}`);
}
+ } finally {
+ await cleanup();
}
}
-
/**
* Generates a thumbnail buffer from a video URL using FFmpeg and Sharp.
*/
@@ -567,71 +544,34 @@ export async function getAudioDetails(bufferOrUrl: Buffer | string): Promise<{
audioCodec: string | null;
audioBitrate: number | null;
} | null> {
- const isUrl = typeof bufferOrUrl === "string";
- let inputPath = "";
- let tempFile = "";
-
- if (isUrl) {
- inputPath = bufferOrUrl;
- } else {
- const tempDir = os.tmpdir();
- const type = await fileTypeFromBuffer(bufferOrUrl);
- const ext = type ? `.${type.ext}` : "";
- tempFile = path.join(tempDir, `temp_audio_details_${generateId()}${ext}`);
- await fs.writeFile(tempFile, bufferOrUrl);
- inputPath = tempFile;
- }
+ const { inputPath, cleanup } = await prepareProbeInput(
+ bufferOrUrl,
+ "temp_audio_details",
+ );
try {
// 1. Check for audio stream and get parameters
- const streamInfo = await new Promise<FFProbeAudioStream | null>(
- (resolve, reject) => {
- const ffprobe = spawn("ffprobe", [
- "-v",
- "error",
- "-select_streams",
- "a:0",
- "-show_entries",
- "stream=codec_name,sample_rate,channels,bits_per_sample,bits_per_raw_sample,bit_rate",
- "-of",
- "json",
- inputPath,
- ]);
-
- let output = "";
- let errorOutput = "";
-
- ffprobe.stdout.on("data", (data) => {
- output += data.toString();
- });
-
- ffprobe.stderr.on("data", (data) => {
- errorOutput += data.toString();
- });
-
- ffprobe.on("error", (err) => {
- reject(new Error(`Failed to start ffprobe: ${err.message}`));
- });
-
- ffprobe.on("close", (code) => {
- if (code === 0) {
- try {
- const data = JSON.parse(output) as FFProbeResult;
- const stream = data.streams?.[0];
- resolve(stream || null);
- } catch (err) {
- reject(new Error(`Failed to parse ffprobe output: ${err}`));
- }
- } else {
- reject(
- new Error(
- `ffprobe exited with code ${code}. stderr: ${errorOutput}`,
- ),
- );
- }
- });
- },
- );
+ const { code, stdout, stderr } = await runFfprobe([
+ "-v",
+ "error",
+ "-select_streams",
+ "a:0",
+ "-show_entries",
+ "stream=codec_name,sample_rate,channels,bits_per_sample,bits_per_raw_sample,bit_rate",
+ "-of",
+ "json",
+ inputPath,
+ ]);
+ if (code !== 0) {
+ throw new Error(`ffprobe exited with code ${code}. stderr: ${stderr}`);
+ }
+ let streamInfo: FFProbeAudioStream | null;
+ try {
+ const data = JSON.parse(stdout) as FFProbeResult;
+ streamInfo = data.streams?.[0] || null;
+ } catch (err) {
+ throw new Error(`Failed to parse ffprobe output: ${err}`);
+ }
if (!streamInfo) {
return null;
@@ -666,15 +606,6 @@ export async function getAudioDetails(bufferOrUrl: Buffer | string): Promise<{
mediaLogger.error({ err }, "Failed to extract audio details");
return null;
} finally {
- if (tempFile) {
- try {
- await fs.unlink(tempFile);
- } catch (err) {
- mediaLogger.error(
- { err },
- "Failed to delete temp file in getAudioDetails",
- );
- }
- }
+ await cleanup("Failed to delete temp file in getAudioDetails");
}
}
diff --git a/packages/node-sdk/package.json b/packages/node-sdk/package.json
index 8de5fe48..f7bbfd8b 100644
--- a/packages/node-sdk/package.json
+++ b/packages/node-sdk/package.json
@@ -23,6 +23,12 @@
"development": "./renderer/index.ts",
"import": "./dist/renderer.mjs",
"default": "./dist/renderer.mjs"
+ },
+ "./gpu": {
+ "types": "./dist/gpu.d.mts",
+ "development": "./renderer/gpu.ts",
+ "import": "./dist/gpu.mjs",
+ "default": "./dist/gpu.mjs"
}
},
"files": [
diff --git a/packages/node-sdk/renderer/gpu.ts b/packages/node-sdk/renderer/gpu.ts
new file mode 100644
index 00000000..5a4143a8
--- /dev/null
+++ b/packages/node-sdk/renderer/gpu.ts
@@ -0,0 +1,346 @@
+/// <reference types="@webgpu/types" />
+import type { SignalData, VirtualMediaData } from "@framefields/core";
+import { signalRegistry } from "@framefields/webgpu-renderers";
+import type { NodeRenderProps, WebGPUNodeRenderer } from "./index.js";
+
+/** The argument object every WebGPU node renderer receives. */
+export type WebGPUNodeRendererArgs = Parameters<WebGPUNodeRenderer>[0];
+
+const TRANSPARENT: GPUColor = { r: 0, g: 0, b: 0, a: 0 };
+
+/**
+ * Full-screen quad vertex stage: a 4-vertex triangle strip (draw(4)) that
+ * hands `in.uv` (0,0 top-left → 1,1 bottom-right) to an `fs` reading `VSOut`.
+ */
+export const FULLSCREEN_QUAD_WGSL = `
+struct VSOut {
+ @builtin(position) pos : vec4<f32>,
+ @location(0) uv : vec2<f32>,
+};
+
+@vertex fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
+ var pos = array<vec2<f32>, 4>(
+ vec2<f32>(-1.0, 1.0),
+ vec2<f32>(1.0, 1.0),
+ vec2<f32>(-1.0, -1.0),
+ vec2<f32>(1.0, -1.0)
+ );
+ var uv = array<vec2<f32>, 4>(
+ vec2<f32>(0.0, 0.0),
+ vec2<f32>(1.0, 0.0),
+ vec2<f32>(0.0, 1.0),
+ vec2<f32>(1.0, 1.0)
+ );
+ return VSOut(vec4<f32>(pos[vi], 0.0, 1.0), uv[vi]);
+}
+`;
+
+/** Premultiplied-alpha "over" blending. */
+export const PREMULTIPLIED_BLEND: GPUBlendState = {
+ color: {
+ srcFactor: "one",
+ dstFactor: "one-minus-src-alpha",
+ operation: "add",
+ },
+ alpha: {
+ srcFactor: "one",
+ dstFactor: "one-minus-src-alpha",
+ operation: "add",
+ },
+};
+
+/** Straight-alpha color over a premultiplied-alpha accumulation. */
+export const STRAIGHT_ALPHA_BLEND: GPUBlendState = {
+ color: {
+ srcFactor: "src-alpha",
+ dstFactor: "one-minus-src-alpha",
+ operation: "add",
+ },
+ alpha: {
+ srcFactor: "one",
+ dstFactor: "one-minus-src-alpha",
+ operation: "add",
+ },
+};
+
+/**
+ * One bind-group entry, visible to the fragment stage unless it says
+ * otherwise: `"uniform"` buffer, `"texture"` (filterable float 2D) or
+ * `"sampler"` (filtering), or any other entry minus its binding index.
+ */
+export type FragmentBinding =
+ | "uniform"
+ | "texture"
+ | "sampler"
+ | Omit<GPUBindGroupLayoutEntry, "binding" | "visibility">
+ | Omit<GPUBindGroupLayoutEntry, "binding">;
+
+/** A bind group layout whose entries are bound at 0, 1, 2… in order. */
+export function fragmentBindGroupLayout(
+ device: GPUDevice,
+ bindings: FragmentBinding[],
+ label?: string,
+): GPUBindGroupLayout {
+ return device.createBindGroupLayout({
+ ...(label && { label }),
+ entries: bindings.map((binding, index): GPUBindGroupLayoutEntry => {
+ if (binding === "uniform") {
+ return {
+ binding: index,
+ visibility: GPUShaderStage.FRAGMENT,
+ buffer: { type: "uniform" },
+ };
+ }
+ if (binding === "texture") {
+ return {
+ binding: index,
+ visibility: GPUShaderStage.FRAGMENT,
+ texture: { sampleType: "float" },
+ };
+ }
+ if (binding === "sampler") {
+ return {
+ binding: index,
+ visibility: GPUShaderStage.FRAGMENT,
+ sampler: { type: "filtering" },
+ };
+ }
+ return {
+ visibility: GPUShaderStage.FRAGMENT,
+ ...binding,
+ binding: index,
+ };
+ }),
+ });
+}
+
+/** `count` float textures followed by one filtering sampler. */
+export function texturesLayout(
+ device: GPUDevice,
+ count: number,
+): GPUBindGroupLayout {
+ return fragmentBindGroupLayout(device, [
+ ...Array<FragmentBinding>(count).fill("texture"),
+ "sampler",
+ ]);
+}
+
+/** Memoizes `create` per GPUDevice (resources can't cross devices). */
+export function perDevice<T>(
+ create: (device: GPUDevice) => T,
+): (device: GPUDevice) => T {
+ const cache = new WeakMap<GPUDevice, T>();
+ return (device) => {
+ let value = cache.get(device);
+ if (value === undefined) {
+ value = create(device);
+ cache.set(device, value);
+ }
+ return value;
+ };
+}
+
+export interface FullscreenPipelineOptions {
+ label?: string;
+ module: GPUShaderModule;
+ layouts: GPUBindGroupLayout[] | GPUPipelineLayout;
+ format: GPUTextureFormat;
+ blend?: GPUBlendState;
+ /** Defaults to "vs". */
+ vertexEntryPoint?: string;
+ /** Defaults to "fs". */
+ fragmentEntryPoint?: string;
+}
+
+/** A triangle-strip pipeline drawing one full-screen quad (`draw(4)`). */
+export function createFullscreenPipeline(
+ device: GPUDevice,
+ options: FullscreenPipelineOptions,
+): GPURenderPipeline {
+ const { module, format, blend } = options;
+ return device.createRenderPipeline({
+ ...(options.label && { label: options.label }),
+ layout: Array.isArray(options.layouts)
+ ? device.createPipelineLayout({ bindGroupLayouts: options.layouts })
+ : options.layouts,
+ vertex: { module, entryPoint: options.vertexEntryPoint ?? "vs" },
+ fragment: {
+ module,
+ entryPoint: options.fragmentEntryPoint ?? "fs",
+ targets: [blend ? { format, blend } : { format }],
+ },
+ primitive: { topology: "triangle-strip" },
+ });
+}
+
+export interface DrawChildToTextureOptions {
+ /** Defaults to the target's size. */
+ width?: number;
+ height?: number;
+ /** Props for the child; defaults to the node's props with the child as media. */
+ props?: Partial<NodeRenderProps>;
+ /** Textures still in use that the temporary texture must not alias. */
+ exclude?: GPUTexture[];
+}
+
+/**
+ * Renders `child` into a cleared temporary texture with an identity
+ * transform and a full-size scissor, and returns that texture.
+ */
+export async function drawChildToTexture(
+ args: WebGPUNodeRendererArgs,
+ child: VirtualMediaData,
+ options: DrawChildToTextureOptions = {},
+): Promise<{ texture: GPUTexture; view: GPUTextureView }> {
+ const { ctx, encoder, props } = args;
+ const width = options.width ?? args.targetWidth;
+ const height = options.height ?? args.targetHeight;
+ const texture = ctx.renderer.getTemporaryTexture(width, height, [
+ ...(props.excludeTextures || []),
+ args.targetTexture,
+ ...(options.exclude ?? []),
+ ]);
+ const view = texture.createView();
+ ctx.renderer
+ .beginFrame(encoder, view, TRANSPARENT, width, height, "clear")
+ .end();
+
+ ctx.renderer.pushScissor({ x: 0, y: 0, width, height });
+ ctx.renderer.pushIdentity();
+ await args.drawChild(
+ child,
+ options.props ?? { ...props, virtualMedia: child },
+ view,
+ texture,
+ width,
+ height,
+ );
+ ctx.renderer.popTransform();
+ ctx.renderer.popScissor();
+ return { texture, view };
+}
+
+/**
+ * Opens a "load" pass on the node's target and makes it the node's current
+ * pass (the caller hands it back to the compositor).
+ */
+export function beginTargetPass(
+ args: WebGPUNodeRendererArgs,
+ width = args.targetWidth,
+ height = args.targetHeight,
+): GPURenderPassEncoder {
+ const pass = args.ctx.renderer.beginFrame(
+ args.encoder,
+ args.targetView,
+ TRANSPARENT,
+ width,
+ height,
+ "load",
+ );
+ args.pass = pass;
+ return pass;
+}
+
+/** Draws `texture` over the whole target at `opacity` (the node's output). */
+export function drawTextureToTarget(
+ args: WebGPUNodeRendererArgs,
+ texture: GPUTexture,
+ opacity = 1,
+): void {
+ const { targetWidth: width, targetHeight: height } = args;
+ args.ctx.renderer.drawTexture(
+ beginTargetPass(args),
+ texture,
+ { x: 0, y: 0, width, height },
+ { opacity },
+ );
+}
+
+/** Clock a signal texture is evaluated at, in seconds. */
+export function signalTiming(
+ props: NodeRenderProps,
+ frame: number,
+ fps: number,
+ fallbackDurationSeconds = 0,
+): { elapsedSeconds: number; durationSeconds: number } {
+ const metadataDurationMs = props.virtualMedia?.metadata?.durationMs;
+ return {
+ elapsedSeconds:
+ props.elapsedMs !== undefined ? props.elapsedMs / 1000 : frame / fps,
+ durationSeconds: metadataDurationMs
+ ? metadataDurationMs / 1000
+ : props.durationMs !== undefined
+ ? props.durationMs / 1000
+ : fallbackDurationSeconds,
+ };
+}
+
+export type NodeSignalData = SignalData & { nodeId?: string };
+
+export interface SignalTextureContext {
+ args: WebGPUNodeRendererArgs;
+ width: number;
+ height: number;
+ frame: number;
+ fps: number;
+ elapsedSeconds: number;
+ durationSeconds: number;
+}
+
+/**
+ * The texture a bound parameter samples: its own GPU binding, the signal's
+ * texture (cached under `sd.nodeId`, else `fallbackId`), or a 1×1 dummy.
+ */
+export function signalTextureView(
+ sc: SignalTextureContext,
+ param: {
+ hasStaticSig: boolean;
+ sd: NodeSignalData | null;
+ gpuTextureView?: GPUTextureView;
+ },
+ fallbackId: string,
+): GPUTextureView {
+ const { args } = sc;
+ if (param.gpuTextureView) return param.gpuTextureView;
+ if (param.hasStaticSig && param.sd) {
+ return signalRegistry.getOrCreate2DTextureView(
+ args.ctx.device,
+ args.encoder,
+ param.sd.nodeId ?? fallbackId,
+ sc.elapsedSeconds,
+ sc.durationSeconds,
+ param.sd,
+ sc.width,
+ sc.height,
+ args.props.renderId,
+ sc.frame,
+ sc.fps,
+ );
+ }
+ return signalRegistry.getDummy1x1TextureView(args.ctx.device);
+}
+
+/** Clears `target` to transparent and draws one full-screen quad into it. */
+export function drawFullscreenPass(
+ encoder: GPUCommandEncoder,
+ target: GPUTextureView,
+ pipeline: GPURenderPipeline,
+ bindGroups: GPUBindGroup[],
+): void {
+ const pass = encoder.beginRenderPass({
+ colorAttachments: [
+ {
+ view: target,
+ loadOp: "clear",
+ storeOp: "store",
+ clearValue: TRANSPARENT,
+ },
+ ],
+ });
+ pass.setPipeline(pipeline);
+ bindGroups.forEach((group, index) => {
+ pass.setBindGroup(index, group);
+ });
+ pass.draw(4);
+ pass.end();
+}
diff --git a/packages/node-sdk/renderer/homography.ts b/packages/node-sdk/renderer/homography.ts
new file mode 100644
index 00000000..4d3b0150
--- /dev/null
+++ b/packages/node-sdk/renderer/homography.ts
@@ -0,0 +1,87 @@
+export interface Point2D {
+ x: number;
+ y: number;
+}
+
+/**
+ * Computes an 8-parameter 3x3 homography matrix mapping destination coordinates (quad)
+ * to normalized source coordinates (unit square [0, 1]^2).
+ *
+ * The homography H maps destination (x, y) to source (u, v):
+ * u = (h00*x + h01*y + h02) / (h20*x + h21*y + h22)
+ * v = (h10*x + h11*y + h12) / (h20*x + h21*y + h22)
+ *
+ * Resulting array: [h00, h01, h02, h10, h11, h12, h20, h21, 1.0]
+ */
+export function solveHomography(
+ dstPoints: Point2D[],
+ srcPoints: Point2D[] = [
+ { x: 0.0, y: 0.0 }, // TL
+ { x: 1.0, y: 0.0 }, // TR
+ { x: 0.0, y: 1.0 }, // BL
+ { x: 1.0, y: 1.0 }, // BR
+ ],
+): number[] {
+ const A: number[][] = [];
+ const B: number[] = [];
+
+ for (let i = 0; i < 4; i++) {
+ const xi = dstPoints[i]?.x ?? 0;
+ const yi = dstPoints[i]?.y ?? 0;
+ const ui = srcPoints[i]?.x ?? 0;
+ const vi = srcPoints[i]?.y ?? 0;
+
+ A.push([xi, yi, 1.0, 0.0, 0.0, 0.0, -xi * ui, -yi * ui]);
+ B.push(ui);
+
+ A.push([0.0, 0.0, 0.0, xi, yi, 1.0, -xi * vi, -yi * vi]);
+ B.push(vi);
+ }
+
+ const n = 8;
+ for (let i = 0; i < n; i++) {
+ // Find pivot row
+ let maxRow = i;
+ let maxVal = Math.abs(A[i][i]);
+ for (let k = i + 1; k < n; k++) {
+ if (Math.abs(A[k][i]) > maxVal) {
+ maxVal = Math.abs(A[k][i]);
+ maxRow = k;
+ }
+ }
+
+ // Swap rows in A and B
+ const tempRow = A[i];
+ A[i] = A[maxRow];
+ A[maxRow] = tempRow;
+
+ const tempB = B[i];
+ B[i] = B[maxRow];
+ B[maxRow] = tempB;
+
+ const pivot = A[i][i];
+ if (Math.abs(pivot) < 1e-12) {
+ A[i][i] = pivot < 0 ? -1e-12 : 1e-12;
+ }
+
+ for (let k = i + 1; k < n; k++) {
+ const factor = A[k][i] / A[i][i];
+ for (let j = i; j < n; j++) {
+ A[k][j] -= factor * A[i][j];
+ }
+ B[k] -= factor * B[i];
+ }
+ }
+
+ // Back-substitution
+ const h = new Array(8).fill(0.0);
+ for (let i = n - 1; i >= 0; i--) {
+ let sum = B[i];
+ for (let j = i + 1; j < n; j++) {
+ sum -= A[i][j] * h[j];
+ }
+ h[i] = sum / A[i][i];
+ }
+
+ return [h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], 1.0];
+}
diff --git a/packages/node-sdk/renderer/index.ts b/packages/node-sdk/renderer/index.ts
index d883a3b8..7901bf85 100644
--- a/packages/node-sdk/renderer/index.ts
+++ b/packages/node-sdk/renderer/index.ts
@@ -83,6 +83,201 @@ export {
resolveString,
} from "@framefields/core";
+/**
+ * A numeric node parameter at `frame`: a number, a signal-like `get(ctx)` or
+ * `{ value }`, a `(ctx) => number` function, or anything `Number()` accepts;
+ * `fallback` when none of those yields a finite number.
+ */
+export function resolveFrameNumber(
+ val: unknown,
+ fallback: number,
+ frame: number,
+ fps: number,
+): number {
+ if (val === undefined || val === null) return fallback;
+ if (typeof val === "number") return Number.isFinite(val) ? val : fallback;
+ if (typeof val === "object" && val !== null) {
+ if (
+ "get" in (val as Record<string, unknown>) &&
+ typeof (val as { get: unknown }).get === "function"
+ ) {
+ try {
+ const res = (val as { get: (ctx?: unknown) => unknown }).get({
+ frame,
+ fps,
+ time: fps > 0 ? frame / fps : 0,
+ duration: 0,
+ durationMs: 0,
+ progress: 0,
+ deltaTime: fps > 0 ? 1 / fps : 0,
+ });
+ const n = Number(res);
+ if (Number.isFinite(n)) return n;
+ } catch {
+ // fallback
+ }
+ }
+ if ("value" in (val as Record<string, unknown>)) {
+ const n = Number((val as { value: unknown }).value);
+ if (Number.isFinite(n)) return n;
+ }
+ }
+ if (typeof val === "function") {
+ try {
+ const n = Number(val({ frame, fps, time: fps > 0 ? frame / fps : 0 }));
+ if (Number.isFinite(n)) return n;
+ } catch {
+ // fallback
+ }
+ }
+ const n = Number(val);
+ return Number.isFinite(n) ? n : fallback;
+}
+
+/**
+ * A static node parameter at `frame`: calls `get(ctx)` / `(frame, fps)`
+ * functions and reads `{ value }` wrappers, keeping a signal's GPU texture
+ * binding when it has one. Anything else is returned as-is.
+ */
+export function unwrapFrameParam(
+ raw: unknown,
+ frame: number,
+ fps: number,
+): { value: unknown; gpuTextureView?: GPUTextureView } {
+ if (raw === undefined || raw === null) return { value: raw };
+ if (typeof raw === "function") {
+ return { value: (raw as (f: number, fps: number) => unknown)(frame, fps) };
+ }
+ if (typeof raw !== "object") return { value: raw };
+ const obj = raw as Record<string, unknown>;
+ if (typeof obj.get === "function") {
+ return {
+ value: (obj.get as (ctx: { frame: number; fps: number }) => unknown)({
+ frame,
+ fps,
+ }),
+ };
+ }
+ if ("value" in obj) {
+ const sig = raw as {
+ value: unknown;
+ gpuBinding?: { textureView?: GPUTextureView };
+ };
+ return { value: sig.value, gpuTextureView: sig.gpuBinding?.textureView };
+ }
+ return { value: raw };
+}
+
+/** The `inputs` map node operations carry: one entry per connected handle. */
+export type NodeOperationInputs = Record<
+ string,
+ {
+ connectionValid: boolean;
+ outputItem: { type: string; data: unknown } | null;
+ }
+>;
+
+export interface HandleParam<SD> {
+ val: number;
+ /** The value comes from a signal (`sd`) sampled on the GPU. */
+ hasStaticSig: boolean;
+ sd: SD | null;
+ handleId: string | null | undefined;
+}
+
+/**
+ * A numeric parameter fed through `handleId`: a connected Number (clamped), a
+ * connected Signal (sampled on the GPU, `val` stays `fallbackVal`), or else
+ * `fallbackVal` clamped to [clampMin, clampMax].
+ */
+export function resolveHandleParam<SD>(
+ inputs: NodeOperationInputs | undefined,
+ handleId: string | null | undefined,
+ fallbackVal: number,
+ clampMin: number,
+ clampMax: number,
+): HandleParam<SD> {
+ const clamp = (v: number) => Math.max(clampMin, Math.min(clampMax, v));
+ const input = handleId ? inputs?.[handleId] : undefined;
+ if (input?.connectionValid) {
+ const outputItem = input.outputItem;
+ if (outputItem?.type === "Number") {
+ const numVal =
+ typeof outputItem.data === "number"
+ ? outputItem.data
+ : ((outputItem.data as { value?: number })?.value ?? fallbackVal);
+ return { val: clamp(numVal), hasStaticSig: false, sd: null, handleId };
+ }
+ if (outputItem?.type === "Signal" && outputItem.data) {
+ return {
+ val: fallbackVal,
+ hasStaticSig: true,
+ sd: outputItem.data as SD,
+ handleId,
+ };
+ }
+ }
+ return { val: clamp(fallbackVal), hasStaticSig: false, sd: null, handleId };
+}
+
+/**
+ * A numeric field `fieldName` of a node operation, fed through its
+ * `${fieldName}HandleId` input: a connected Signal/Numeric (`sd`, sampled on
+ * the GPU; `value` is its offset), a connected Number, or the operation's own
+ * value — each clamped to [minVal, maxVal].
+ */
+export function resolveSignalField<SD>(
+ op: { inputs?: NodeOperationInputs },
+ fieldName: string,
+ defaultValue: number,
+ minVal = Number.NEGATIVE_INFINITY,
+ maxVal = Number.POSITIVE_INFINITY,
+): { hasSignal: boolean; sd: SD | null; value: number } {
+ const clamp = (v: number) => Math.max(minVal, Math.min(maxVal, v));
+ const fields = op as Record<string, unknown>;
+ const handleId = fields[`${fieldName}HandleId`] as string | null | undefined;
+ const signalInput = handleId ? op.inputs?.[handleId] : null;
+ const outputType = signalInput?.connectionValid
+ ? signalInput.outputItem?.type
+ : undefined;
+ const hasSignal = outputType === "Signal" || outputType === "Numeric";
+ const sd =
+ hasSignal && signalInput?.outputItem?.data
+ ? (signalInput.outputItem.data as SD)
+ : null;
+
+ let value = defaultValue;
+ if (!hasSignal) {
+ const raw =
+ outputType === "Number"
+ ? signalInput?.outputItem?.data
+ : fields[fieldName];
+ value = clamp(Number(raw ?? defaultValue));
+ } else if (sd) {
+ value = clamp(Number((sd as { offset?: unknown }).offset ?? defaultValue));
+ }
+ return { hasSignal, sd, value };
+}
+
+/**
+ * Whether the handle whose id is stored at `op[handleIdKey]` has a valid
+ * Signal or Number connection.
+ */
+export function isHandleConnected(
+ op: Record<string, unknown>,
+ handleIdKey: string,
+): boolean {
+ const handleId = op[handleIdKey];
+ if (typeof handleId !== "string" || !handleId) return false;
+ const input = (op.inputs as NodeOperationInputs | undefined)?.[handleId];
+ return Boolean(
+ input?.connectionValid &&
+ input.outputItem &&
+ (input.outputItem.type === "Signal" ||
+ input.outputItem.type === "Number"),
+ );
+}
+
export interface NodeRendererPlugin {
WebGPURenderer?: WebGPUNodeRenderer;
audioProcessor?: AudioProcessor;
@@ -151,3 +346,5 @@ export const audioRegistry: AudioProcessorRegistry = (() => {
}
return g[GLOBAL_AUDIO_KEY];
})();
+
+export * from "./homography.js";
diff --git a/packages/node-sdk/shared/schemas.ts b/packages/node-sdk/shared/schemas.ts
index a6123c84..51db55ef 100644
--- a/packages/node-sdk/shared/schemas.ts
+++ b/packages/node-sdk/shared/schemas.ts
@@ -115,3 +115,18 @@ export const SignalResultSchema = SingleOutputGenericSchema(
createOutputItemSchema(z.literal("Signal"), z.unknown()),
);
export type SignalResult = z.infer<typeof SignalResultSchema>;
+
+/** The `inputs` map an operation carries: one entry per connected handle. */
+export const OperationInputsSchema = z.record(
+ z.string(),
+ z.object({
+ connectionValid: z.boolean(),
+ outputItem: z
+ .object({
+ type: z.string(),
+ data: z.unknown(),
+ })
+ .nullable()
+ .optional(),
+ }),
+);
diff --git a/packages/node-sdk/tsdown.config.ts b/packages/node-sdk/tsdown.config.ts
index fb691c8f..6659d1f4 100644
--- a/packages/node-sdk/tsdown.config.ts
+++ b/packages/node-sdk/tsdown.config.ts
@@ -4,6 +4,7 @@ export default defineConfig({
entry: {
index: "index.ts",
renderer: "renderer/index.ts",
+ gpu: "renderer/gpu.ts",
},
format: ["esm"],
dts: true,
diff --git a/packages/renderer/src/video-worker.ts b/packages/renderer/src/video-worker.ts
index 8f2e88bd..4e5ead87 100644
--- a/packages/renderer/src/video-worker.ts
+++ b/packages/renderer/src/video-worker.ts
@@ -7,11 +7,14 @@ import type { VirtualMediaData } from "@framefields/core";
import { rendererLogger } from "@framefields/server-utils";
import {
clearAllVideoCache,
+ createHeadlessDOMElement,
ensureDevice,
initHeadlessWebGPU,
+ installIntersectionObserverStub,
lutStore,
mediaDecoderCache,
NodeSurfaceProvider,
+ patchCanvasPrototypeForDOM,
type RenderContextValue,
Renderer2D,
resetDeviceInstance,
@@ -40,26 +43,7 @@ if (typeof globalThis.requestAnimationFrame === "undefined") {
globalObj.cancelAnimationFrame = (id: number) => clearTimeout(id);
}
-const dummyDomElement = {
- clientWidth: 1920,
- clientHeight: 1080,
- getBoundingClientRect: () => ({
- x: 0,
- y: 0,
- left: 0,
- top: 0,
- right: 1920,
- bottom: 1080,
- width: 1920,
- height: 1080,
- }),
- style: {},
- appendChild: () => {},
- removeChild: () => {},
- addEventListener: () => {},
- removeEventListener: () => {},
- getAttribute: () => null,
-};
+const dummyDomElement = createHeadlessDOMElement();
if (typeof globalThis.OffscreenCanvas === "undefined") {
globalObj.OffscreenCanvas = SkiaCanvas;
@@ -77,13 +61,7 @@ if (typeof globalThis.window === "undefined") {
globalObj.window = globalThis;
}
-if (typeof globalThis.IntersectionObserver === "undefined") {
- globalObj.IntersectionObserver = class IntersectionObserver {
- observe() {}
- unobserve() {}
- disconnect() {}
- };
-}
+installIntersectionObserverStub();
if (typeof globalThis.document === "undefined") {
globalObj.document = {
@@ -105,69 +83,7 @@ if (typeof globalThis.document === "undefined") {
}
const skiaProto = SkiaCanvas.prototype as unknown as Record<string, unknown>;
-if (typeof skiaProto.getBoundingClientRect === "undefined") {
- skiaProto.getBoundingClientRect = function (this: {
- width?: number;
- height?: number;
- }) {
- const width = this.width || 512;
- const height = this.height || 512;
- return {
- x: 0,
- y: 0,
- left: 0,
- top: 0,
- right: width,
- bottom: height,
- width,
- height,
- };
- };
-}
-
-if (!("style" in skiaProto)) {
- Object.defineProperty(skiaProto, "style", {
- get(this: { _style?: Record<string, unknown> }) {
- this._style ??= {};
- return this._style;
- },
- configurable: true,
- });
-}
-
-if (!("clientWidth" in skiaProto)) {
- Object.defineProperty(skiaProto, "clientWidth", {
- get(this: { width?: number }) {
- return this.width || 512;
- },
- configurable: true,
- });
-}
-
-if (!("clientHeight" in skiaProto)) {
- Object.defineProperty(skiaProto, "clientHeight", {
- get(this: { height?: number }) {
- return this.height || 512;
- },
- configurable: true,
- });
-}
-
-if (!("parentElement" in skiaProto)) {
- Object.defineProperty(skiaProto, "parentElement", {
- get() {
- return dummyDomElement;
- },
- configurable: true,
- });
-}
-
-if (typeof skiaProto.addEventListener === "undefined") {
- skiaProto.addEventListener = () => {};
-}
-if (typeof skiaProto.removeEventListener === "undefined") {
- skiaProto.removeEventListener = () => {};
-}
+patchCanvasPrototypeForDOM(skiaProto, dummyDomElement);
if (typeof skiaProto.getAttribute === "undefined") {
skiaProto.getAttribute = () => null;
}
diff --git a/packages/renderers/src/index.ts b/packages/renderers/src/index.ts
index 77904394..6612a3eb 100644
--- a/packages/renderers/src/index.ts
+++ b/packages/renderers/src/index.ts
@@ -1,7 +1,7 @@
+export * from "@framefields/webgpu-renderers/math3d";
export * from "./blend.js";
export * from "./geometry.js";
export * from "./interpolate.js";
-export * from "./math3d/index.js";
export * from "./preload-fonts.js";
export * from "./spring.js";
export * from "./text-measurer.js";
diff --git a/packages/renderers/src/math3d/camera3d.ts b/packages/renderers/src/math3d/camera3d.ts
deleted file mode 100644
index 9d798cab..00000000
--- a/packages/renderers/src/math3d/camera3d.ts
+++ /dev/null
@@ -1,442 +0,0 @@
-/**
- * Camera 3D mathematics, kinematics, and projection solver.
- * Provides unified View-Projection matrices, LookAt target tracking,
- * spherical orbit turntable, multi-octave camera shake, and rack-focus CoC solvers.
- */
-
-import { type Mat4, Matrix4Math } from "./matrix4.js";
-import { type Vec3, Vector3Math } from "./vector3.js";
-
-export interface Camera3DPose {
- /** Camera Eye position in world space [x, y, z] */
- eye: Vec3;
- /** LookAt Target / Point of Interest [x, y, z] */
- target: Vec3;
- /** Camera Up vector (default [0, -1, 0] for Y-down screen coordinates) */
- up: Vec3;
- /** Vertical field of view in degrees (default 50) */
- fov: number;
- /** Viewport aspect ratio (width / height) */
- aspect: number;
- /** Near clipping plane distance (default 1.0) */
- near: number;
- /** Far clipping plane distance (default 50000.0) */
- far: number;
- /** Optical zoom multiplier (default 1.0) */
- zoom: number;
- /** Focal plane distance from camera for Depth of Field in pixels */
- focusDistance?: number;
- /** Lens f-number / aperture (e.g. 1.4, 2.8, 5.6) */
- fStop?: number;
- /** Maximum bokeh blur radius in pixels (default 32) */
- maxBlurRadius?: number;
-}
-
-export interface CameraShakeOptions {
- translationAmplitude: number;
- rotationAmplitude: number;
- frequency?: number;
- octaves?: number;
- seed?: number;
-}
-
-export type FrustumPlane = [number, number, number, number]; // [a, b, c, d] where ax + by + cz + d >= 0
-
-export interface FrustumPlanes {
- left: FrustumPlane;
- right: FrustumPlane;
- bottom: FrustumPlane;
- top: FrustumPlane;
- near: FrustumPlane;
- far: FrustumPlane;
-}
-
-/** Deterministic pseudo-random gradient noise generator */
-function pseudoNoise(t: number, seed: number): number {
- const n = Math.sin(t * 12.9898 + seed * 78.233) * 43758.5453123;
- const frac = n - Math.floor(n);
- return frac * 2.0 - 1.0;
-}
-
-/** Smooth multi-harmonic procedural continuous noise */
-function smoothHarmonicNoise(t: number, seed: number, octaves = 3): number {
- let total = 0;
- let frequency = 1.0;
- let amplitude = 1.0;
- let maxAmp = 0;
-
- for (let i = 0; i < octaves; i++) {
- const s1 = Math.sin(t * frequency * 2.0 * Math.PI + seed + i * 1.7);
- const s2 = Math.cos(t * frequency * 1.414 * Math.PI + seed * 2.1 + i * 0.9);
- total += (s1 * 0.6 + s2 * 0.4) * amplitude;
- maxAmp += amplitude;
- frequency *= 2.0;
- amplitude *= 0.5;
- }
-
- return maxAmp > 0 ? total / maxAmp : 0;
-}
-
-export const Camera3D = {
- /**
- * Creates a default canvas-aligned 3D camera.
- * Calibrated so that layers at Z=0 project 1:1 with 2D canvas pixel coordinates.
- */
- createDefaultCamera(
- width: number,
- height: number,
- fovDegrees = 50,
- ): Camera3DPose {
- const fovRad = (fovDegrees * Math.PI) / 180;
- const distance = height / 2.0 / Math.tan(fovRad / 2.0);
-
- return {
- eye: [width / 2.0, height / 2.0, -distance],
- target: [width / 2.0, height / 2.0, 0],
- up: [0, -1, 0], // Y-down to match 2D canvas
- fov: fovDegrees,
- aspect: Math.max(1e-4, width / height),
- near: 1.0,
- far: 50000.0,
- zoom: 1.0,
- focusDistance: distance,
- fStop: 2.8,
- maxBlurRadius: 32,
- };
- },
-
- /**
- * Computes LookAt view matrix from camera pose.
- */
- computeViewMatrix(camera: Camera3DPose): Mat4 {
- return Matrix4Math.lookAt(camera.eye, camera.target, camera.up);
- },
-
- /**
- * Computes WebGPU [0, 1] perspective projection matrix with zoom factor.
- */
- computeProjectionMatrix(camera: Camera3DPose): Mat4 {
- const fovRad = (camera.fov * Math.PI) / 180;
- const p = Matrix4Math.perspective(
- fovRad,
- camera.aspect,
- camera.near,
- camera.far,
- );
-
- if (camera.zoom !== 1.0 && camera.zoom > 0) {
- p[0] *= camera.zoom;
- p[5] *= camera.zoom;
- }
-
- return p;
- },
-
- /**
- * Computes combined View-Projection matrix: VP = P x V.
- */
- computeViewProjectionMatrix(camera: Camera3DPose): Mat4 {
- const v = Camera3D.computeViewMatrix(camera);
- const p = Camera3D.computeProjectionMatrix(camera);
- return Matrix4Math.multiply(p, v);
- },
-
- /**
- * Dolly camera along optical viewing axis.
- * Positive delta moves eye closer to target.
- */
- dolly(
- eye: Vec3,
- target: Vec3,
- deltaDistance: number,
- ): { eye: Vec3; target: Vec3 } {
- const forward = Vector3Math.normalize(Vector3Math.subtract(target, eye));
- const move = Vector3Math.multiplyScalar(forward, deltaDistance);
- return {
- eye: Vector3Math.add(eye, move),
- target,
- };
- },
-
- /**
- * Truck (lateral X) and Pedestal (vertical Y) camera translation in local camera space.
- */
- truckPedestal(
- eye: Vec3,
- target: Vec3,
- up: Vec3,
- deltaX: number,
- deltaY: number,
- ): { eye: Vec3; target: Vec3 } {
- const forward = Vector3Math.normalize(Vector3Math.subtract(target, eye));
- const right = Vector3Math.normalize(Vector3Math.cross(forward, up));
- const trueUp = Vector3Math.cross(right, forward);
-
- const moveX = Vector3Math.multiplyScalar(right, deltaX);
- const moveY = Vector3Math.multiplyScalar(trueUp, deltaY);
- const totalMove = Vector3Math.add(moveX, moveY);
-
- return {
- eye: Vector3Math.add(eye, totalMove),
- target: Vector3Math.add(target, totalMove),
- };
- },
-
- /**
- * Computes camera Eye position orbiting around target in spherical coordinates.
- * @param target Center point of orbit.
- * @param radius Distance from target.
- * @param azimuthDeg Horizontal orbit angle in degrees.
- * @param elevationDeg Vertical elevation angle in degrees (from horizon).
- */
- orbit(
- target: Vec3,
- radius: number,
- azimuthDeg: number,
- elevationDeg: number,
- ): Vec3 {
- const azRad = (azimuthDeg * Math.PI) / 180;
- const elRad = (elevationDeg * Math.PI) / 180;
-
- const cosEl = Math.cos(elRad);
- const sinEl = Math.sin(elRad);
- const cosAz = Math.cos(azRad);
- const sinAz = Math.sin(azRad);
-
- const x = target[0] + radius * cosEl * sinAz;
- const y = target[1] - radius * sinEl; // negative for Y-down coordinates
- const z = target[2] - radius * cosEl * cosAz;
-
- return [x, y, z];
- },
- /**
- * Applies Euler orientation angles (pitch, yaw, roll) in degrees to a camera pose.
- * Pitch rotates around camera local Right axis (tilt up/down).
- * Yaw rotates around camera local Up axis (pan left/right).
- * Roll rotates around camera optical Forward axis (bank counter/clockwise).
- */
- applyOrientation(
- pose: Camera3DPose,
- pitchDeg: number,
- yawDeg: number,
- rollDeg: number,
- ): Camera3DPose {
- if (pitchDeg === 0 && yawDeg === 0 && rollDeg === 0) {
- return pose;
- }
-
- const forward = Vector3Math.normalize(
- Vector3Math.subtract(pose.target, pose.eye),
- );
- const right = Vector3Math.normalize(Vector3Math.cross(forward, pose.up));
- const trueUp = Vector3Math.cross(right, forward);
- const dist = Vector3Math.distance(pose.eye, pose.target);
-
- let fwd = forward;
- let up = trueUp;
-
- // 1. Yaw (pan around Up)
- if (yawDeg !== 0) {
- const yawRad = (yawDeg * Math.PI) / 180;
- const cosY = Math.cos(yawRad);
- const sinY = Math.sin(yawRad);
- fwd = Vector3Math.normalize(
- Vector3Math.subtract(
- Vector3Math.multiplyScalar(fwd, cosY),
- Vector3Math.multiplyScalar(right, sinY),
- ),
- );
- }
-
- // Recompute right after yaw
- const currentRight = Vector3Math.normalize(Vector3Math.cross(fwd, up));
-
- // 2. Pitch (tilt around Right)
- if (pitchDeg !== 0) {
- const pitchRad = (pitchDeg * Math.PI) / 180;
- const cosP = Math.cos(pitchRad);
- const sinP = Math.sin(pitchRad);
- fwd = Vector3Math.normalize(
- Vector3Math.add(
- Vector3Math.multiplyScalar(fwd, cosP),
- Vector3Math.multiplyScalar(up, sinP),
- ),
- );
- up = Vector3Math.normalize(Vector3Math.cross(currentRight, fwd));
- }
-
- // 3. Roll (bank around optical axis fwd)
- if (rollDeg !== 0) {
- const rollRad = (rollDeg * Math.PI) / 180;
- const cosR = Math.cos(rollRad);
- const sinR = Math.sin(rollRad);
- up = Vector3Math.normalize(
- Vector3Math.add(
- Vector3Math.multiplyScalar(up, cosR),
- Vector3Math.multiplyScalar(currentRight, sinR),
- ),
- );
- }
-
- const newTarget = Vector3Math.add(
- pose.eye,
- Vector3Math.multiplyScalar(fwd, dist),
- );
-
- return {
- ...pose,
- target: newTarget,
- up,
- };
- },
-
- /**
- * Applies continuous procedural 3D camera shake (Simplex/harmonic noise).
- */
- applyCameraShake(
- pose: Camera3DPose,
- timeSeconds: number,
- options: CameraShakeOptions,
- ): Camera3DPose {
- const {
- translationAmplitude,
- rotationAmplitude,
- frequency = 2.5,
- octaves = 3,
- seed = 42,
- } = options;
-
- if (translationAmplitude <= 0 && rotationAmplitude <= 0) {
- return pose;
- }
-
- const t = timeSeconds * frequency;
- const dx = smoothHarmonicNoise(t, seed, octaves) * translationAmplitude;
- const dy =
- smoothHarmonicNoise(t, seed + 101.5, octaves) * translationAmplitude;
- const dz =
- smoothHarmonicNoise(t, seed + 203.7, octaves) *
- (translationAmplitude * 0.5);
-
- const rotX =
- smoothHarmonicNoise(t, seed + 307.1, octaves) * rotationAmplitude;
- const rotY =
- smoothHarmonicNoise(t, seed + 409.3, octaves) * rotationAmplitude;
-
- const forward = Vector3Math.normalize(
- Vector3Math.subtract(pose.target, pose.eye),
- );
- const right = Vector3Math.normalize(Vector3Math.cross(forward, pose.up));
- const trueUp = Vector3Math.cross(right, forward);
-
- const disp = Vector3Math.add(
- Vector3Math.multiplyScalar(right, dx),
- Vector3Math.add(
- Vector3Math.multiplyScalar(trueUp, dy),
- Vector3Math.multiplyScalar(forward, dz),
- ),
- );
-
- const newEye = Vector3Math.add(pose.eye, disp);
- const newTarget = Vector3Math.add(
- pose.target,
- Vector3Math.add(
- Vector3Math.multiplyScalar(right, rotY * 10),
- Vector3Math.multiplyScalar(trueUp, rotX * 10),
- ),
- );
-
- return {
- ...pose,
- eye: newEye,
- target: newTarget,
- };
- },
-
- /**
- * Computes Circle of Confusion (CoC) blur radius for Depth of Field.
- * Points at depth == focusDistance produce 0.0 (sharp).
- */
- computeCircleOfConfusion(
- depth: number,
- focusDistance: number,
- focalLengthMm: number,
- fStop: number,
- maxBlurRadius = 32,
- ): number {
- if (focusDistance <= 0 || fStop <= 0 || depth <= 0) return 0;
- const delta = Math.abs(depth - focusDistance);
- const aperture = focalLengthMm / fStop;
- const coc = (delta / depth) * aperture * 0.5;
- return Math.min(maxBlurRadius, Math.max(0, coc));
- },
-
- /**
- * Extracts the 6 frustum clipping planes from a View-Projection matrix.
- */
- computeCameraFrustumPlanes(vp: Mat4): FrustumPlanes {
- const normalizePlane = (p: FrustumPlane): FrustumPlane => {
- const len = Math.hypot(p[0], p[1], p[2]);
- if (len === 0) return p;
- return [p[0] / len, p[1] / len, p[2] / len, p[3] / len];
- };
-
- return {
- left: normalizePlane([
- vp[3] + vp[0],
- vp[7] + vp[4],
- vp[11] + vp[8],
- vp[15] + vp[12],
- ]),
- right: normalizePlane([
- vp[3] - vp[0],
- vp[7] - vp[4],
- vp[11] - vp[8],
- vp[15] - vp[12],
- ]),
- bottom: normalizePlane([
- vp[3] + vp[1],
- vp[7] + vp[5],
- vp[11] + vp[9],
- vp[15] + vp[13],
- ]),
- top: normalizePlane([
- vp[3] - vp[1],
- vp[7] - vp[5],
- vp[11] - vp[9],
- vp[15] - vp[13],
- ]),
- near: normalizePlane([vp[2], vp[6], vp[10], vp[14]]),
- far: normalizePlane([
- vp[3] - vp[2],
- vp[7] - vp[6],
- vp[11] - vp[10],
- vp[15] - vp[14],
- ]),
- };
- },
-
- /**
- * Checks whether an axis-aligned 3D bounding box intersects the camera frustum.
- */
- isBoxInFrustum(min: Vec3, max: Vec3, planes: FrustumPlanes): boolean {
- const planeList: FrustumPlane[] = [
- planes.left,
- planes.right,
- planes.bottom,
- planes.top,
- planes.near,
- planes.far,
- ];
-
- for (const p of planeList) {
- const px = p[0] > 0 ? max[0] : min[0];
- const py = p[1] > 0 ? max[1] : min[1];
- const pz = p[2] > 0 ? max[2] : min[2];
- if (p[0] * px + p[1] * py + p[2] * pz + p[3] < 0) {
- return false;
- }
- }
- return true;
- },
-};
diff --git a/packages/renderers/src/math3d/index.ts b/packages/renderers/src/math3d/index.ts
deleted file mode 100644
index 062727b4..00000000
--- a/packages/renderers/src/math3d/index.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export * from "./camera3d.js";
-export * from "./light3d.js";
-export * from "./matrix4.js";
-export * from "./path3d.js";
-export * from "./vector3.js";
diff --git a/packages/renderers/src/math3d/light3d.ts b/packages/renderers/src/math3d/light3d.ts
deleted file mode 100644
index 20d9f807..00000000
--- a/packages/renderers/src/math3d/light3d.ts
+++ /dev/null
@@ -1,308 +0,0 @@
-/**
- * WebGPU 3D Lighting & Material Shading engine.
- * Supports Ambient, Directional, Point, and Spot light sources,
- * normal matrix computation for non-uniform surface scaling,
- * and binary uniform buffer packing for WGSL shaders.
- */
-
-import { type Mat4, Matrix4Math } from "./matrix4.js";
-import { type Vec3, Vector3Math } from "./vector3.js";
-
-export type LightKind = "ambient" | "directional" | "point" | "spot";
-
-export interface Light3DOptions {
- id?: string;
- lightType?: LightKind;
- type?: LightKind;
- color?:
- | string
- | [number, number, number]
- | [number, number, number, number]
- | { r: number; g: number; b: number; a?: number }
- | unknown;
- intensity?: number;
- x?: number;
- y?: number;
- z?: number;
- position?: [number, number, number];
- targetX?: number;
- targetY?: number;
- targetZ?: number;
- target?: [number, number, number];
- direction?: [number, number, number];
- radius?: number; // Distance cutoff / range
- decay?: number; // Attenuation exponent (default 2.0)
- angle?: number; // Outer cone half-angle in degrees (default 45)
- penumbra?: number; // Softness fraction [0, 1] (default 0.2)
-}
-
-export interface GPULightData {
- /** xyz = position, w = type (0=ambient, 1=directional, 2=point, 3=spot) */
- posType: [number, number, number, number];
- /** xyz = normalized direction, w = radius */
- dirRadius: [number, number, number, number];
- /** rgb = linear color, w = intensity */
- colorIntensity: [number, number, number, number];
- /** x = cos(outer), y = cos(inner), z = decay, w = 0 */
- spotParams: [number, number, number, number];
-}
-
-export const MAX_LIGHTS = 8;
-export const LIGHT_UNIFORM_SIZE_BYTES = 544; // 16 (ambient) + 16 (params) + 8 * 64 (lights)
-
-export interface LightsUniformData {
- ambientColor: [number, number, number, number];
- params: [number, number, number, number];
- lights: GPULightData[];
-}
-
-/**
- * Parses CSS / hex / rgb color strings, arrays, or color objects into normalized linear [r, g, b] floats in [0, 1].
- */
-export function parseColorToRgb(
- color?:
- | string
- | [number, number, number]
- | [number, number, number, number]
- | { r: number; g: number; b: number; a?: number }
- | unknown,
-): [number, number, number] {
- if (!color) return [1, 1, 1];
- if (Array.isArray(color)) {
- const r = color[0] ?? 1;
- const g = color[1] ?? 1;
- const b = color[2] ?? 1;
- return [r > 1 ? r / 255 : r, g > 1 ? g / 255 : g, b > 1 ? b / 255 : b];
- }
- if (typeof color === "object" && color !== null) {
- const r = (color as { r?: number }).r ?? 1;
- const g = (color as { g?: number }).g ?? 1;
- const b = (color as { b?: number }).b ?? 1;
- return [r > 1 ? r / 255 : r, g > 1 ? g / 255 : g, b > 1 ? b / 255 : b];
- }
- if (typeof color !== "string") return [1, 1, 1];
- const str = color.trim().toLowerCase();
-
- if (str.startsWith("#")) {
- const hex = str.slice(1);
- if (hex.length === 3) {
- const r = Number.parseInt(hex[0] + hex[0], 16) / 255;
- const g = Number.parseInt(hex[1] + hex[1], 16) / 255;
- const b = Number.parseInt(hex[2] + hex[2], 16) / 255;
- return [r, g, b];
- }
- if (hex.length >= 6) {
- const r = Number.parseInt(hex.slice(0, 2), 16) / 255;
- const g = Number.parseInt(hex.slice(2, 4), 16) / 255;
- const b = Number.parseInt(hex.slice(4, 6), 16) / 255;
- return [r, g, b];
- }
- }
-
- const rgbMatch = str.match(
- /rgba?\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/,
- );
- if (rgbMatch) {
- const r = Number.parseFloat(rgbMatch[1]) / 255;
- const g = Number.parseFloat(rgbMatch[2]) / 255;
- const b = Number.parseFloat(rgbMatch[3]) / 255;
- return [
- Math.max(0, Math.min(1, r)),
- Math.max(0, Math.min(1, g)),
- Math.max(0, Math.min(1, b)),
- ];
- }
-
- const hslMatch = str.match(
- /hsla?\s*\(\s*([\d.]+)\s*,\s*([\d.]+)%?\s*,\s*([\d.]+)%?/,
- );
- if (hslMatch) {
- const h = Number.parseFloat(hslMatch[1]) / 360;
- const s = Number.parseFloat(hslMatch[2]) / 100;
- const l = Number.parseFloat(hslMatch[3]) / 100;
- return hslToRgb(h, s, l);
- }
-
- return [1, 1, 1];
-}
-
-function hslToRgb(h: number, s: number, l: number): [number, number, number] {
- if (s === 0) return [l, l, l];
- const hue2rgb = (p: number, q: number, t: number) => {
- let tt = t;
- if (tt < 0) tt += 1;
- if (tt > 1) tt -= 1;
- if (tt < 1 / 6) return p + (q - p) * 6 * tt;
- if (tt < 1 / 2) return q;
- if (tt < 2 / 3) return p + (q - p) * (2 / 3 - tt) * 6;
- return p;
- };
- const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
- const p = 2 * l - q;
- return [hue2rgb(p, q, h + 1 / 3), hue2rgb(p, q, h), hue2rgb(p, q, h - 1 / 3)];
-}
-
-export const Light3D = {
- /**
- * Computes the 3x3 normal matrix (stored in 4x4 layout) from a 4x4 Model matrix.
- * Evaluates N = (M^-1)^T to preserve true orthogonal surface normals under non-uniform scaling.
- */
- computeNormalMatrix(modelMatrix: Mat4): Mat4 {
- const inv = Matrix4Math.invert(modelMatrix);
- if (!inv) {
- return Matrix4Math.identity();
- }
- // Transpose of the inverted matrix
- const normMat: Mat4 = [
- inv[0],
- inv[4],
- inv[8],
- 0,
- inv[1],
- inv[5],
- inv[9],
- 0,
- inv[2],
- inv[6],
- inv[10],
- 0,
- 0,
- 0,
- 0,
- 1,
- ];
- return normMat;
- },
-
- /**
- * Converts high-level light options into uniform data ready for GPU packing.
- */
- processLight(opt: Light3DOptions): GPULightData {
- const color = parseColorToRgb(opt.color);
- const intensity = opt.intensity ?? 1.0;
- const posX = opt.position ? opt.position[0] : (opt.x ?? 0);
- const posY = opt.position ? opt.position[1] : (opt.y ?? 0);
- const posZ = opt.position ? opt.position[2] : (opt.z ?? 0);
-
- const kind = opt.lightType ?? opt.type ?? "directional";
- let typeVal = 1.0;
- if (kind === "ambient") typeVal = 0.0;
- else if (kind === "directional") typeVal = 1.0;
- else if (kind === "point") typeVal = 2.0;
- else if (kind === "spot") typeVal = 3.0;
-
- let dir: Vec3 = [0, 0, -1];
- if (opt.direction) {
- dir = Vector3Math.normalize(opt.direction);
- } else if (
- opt.target ||
- opt.targetX !== undefined ||
- opt.targetY !== undefined ||
- opt.targetZ !== undefined
- ) {
- const targetX = opt.target ? opt.target[0] : (opt.targetX ?? posX);
- const targetY = opt.target ? opt.target[1] : (opt.targetY ?? posY);
- const targetZ = opt.target ? opt.target[2] : (opt.targetZ ?? posZ);
- const target: Vec3 = [targetX, targetY, targetZ];
- const fromLight = Vector3Math.subtract(target, [posX, posY, posZ]);
- const len = Vector3Math.length(fromLight);
- if (len > 1e-4) {
- dir = Vector3Math.normalize(fromLight);
- }
- }
-
- const radius = Math.max(1.0, opt.radius ?? 1000);
- const decay = opt.decay ?? 2.0;
- const angleDeg = Math.max(0.1, Math.min(180, opt.angle ?? 45));
- const penumbraFrac = Math.max(0, Math.min(1, opt.penumbra ?? 0.2));
-
- const outerRad = (angleDeg * Math.PI) / 180;
- const innerRad = outerRad * (1 - penumbraFrac);
- const cosOuter = Math.cos(outerRad);
- const cosInner = Math.cos(innerRad);
-
- return {
- posType: [posX, posY, posZ, typeVal],
- dirRadius: [dir[0], dir[1], dir[2], radius],
- colorIntensity: [color[0], color[1], color[2], intensity],
- spotParams: [cosOuter, cosInner, decay, 0.0],
- };
- },
-
- /**
- * Packs active scene lights into a Float32Array matching WGSL LightsUniforms layout.
- */
- packLightsUniforms(lights: Light3DOptions[]): Float32Array {
- const buffer = new Float32Array(LIGHT_UNIFORM_SIZE_BYTES / 4);
-
- if (!lights || lights.length === 0) {
- buffer[4] = 0.0;
- buffer[5] = 0.0;
- return buffer;
- }
-
- let ambientR = 0;
- let ambientG = 0;
- let ambientB = 0;
-
- const directLights: GPULightData[] = [];
-
- for (const l of lights) {
- const kind = l.lightType ?? l.type;
- if (kind === "ambient") {
- const col = parseColorToRgb(l.color);
- const int = l.intensity ?? 1.0;
- ambientR += col[0] * int;
- ambientG += col[1] * int;
- ambientB += col[2] * int;
- } else {
- if (directLights.length < MAX_LIGHTS) {
- directLights.push(Light3D.processLight(l));
- }
- }
- }
-
- buffer[0] = ambientR;
- buffer[1] = ambientG;
- buffer[2] = ambientB;
- buffer[3] = 1.0;
-
- buffer[4] = directLights.length;
- buffer[5] = 1.0;
- buffer[6] = 0.0;
- buffer[7] = 0.0;
-
- let offset = 8;
- for (let i = 0; i < MAX_LIGHTS; i++) {
- if (i < directLights.length) {
- const dl = directLights[i];
- buffer[offset + 0] = dl.posType[0];
- buffer[offset + 1] = dl.posType[1];
- buffer[offset + 2] = dl.posType[2];
- buffer[offset + 3] = dl.posType[3];
-
- buffer[offset + 4] = dl.dirRadius[0];
- buffer[offset + 5] = dl.dirRadius[1];
- buffer[offset + 6] = dl.dirRadius[2];
- buffer[offset + 7] = dl.dirRadius[3];
-
- buffer[offset + 8] = dl.colorIntensity[0];
- buffer[offset + 9] = dl.colorIntensity[1];
- buffer[offset + 10] = dl.colorIntensity[2];
- buffer[offset + 11] = dl.colorIntensity[3];
-
- buffer[offset + 12] = dl.spotParams[0];
- buffer[offset + 13] = dl.spotParams[1];
- buffer[offset + 14] = dl.spotParams[2];
- buffer[offset + 15] = dl.spotParams[3];
- } else {
- for (let j = 0; j < 16; j++) {
- buffer[offset + j] = 0.0;
- }
- }
- offset += 16;
- }
-
- return buffer;
- },
-};
diff --git a/packages/renderers/src/math3d/matrix4.ts b/packages/renderers/src/math3d/matrix4.ts
deleted file mode 100644
index bb545648..00000000
--- a/packages/renderers/src/math3d/matrix4.ts
+++ /dev/null
@@ -1,424 +0,0 @@
-/**
- * Column-major 4x4 matrix mathematics for WebGPU 3D rendering and camera projections.
- * Memory layout matches WGSL `mat4x4<f32>`:
- * [ 0, 4, 8, 12 ]
- * [ 1, 5, 9, 13 ]
- * [ 2, 6, 10, 14 ]
- * [ 3, 7, 11, 15 ]
- */
-
-import { type Vec3, Vector3Math } from "./vector3.js";
-
-export type Mat4 = [
- number,
- number,
- number,
- number, // col 0
- number,
- number,
- number,
- number, // col 1
- number,
- number,
- number,
- number, // col 2
- number,
- number,
- number,
- number, // col 3
-];
-
-export const Matrix4Math = {
- identity(): Mat4 {
- return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
- },
-
- copy(m: Mat4): Mat4 {
- return [...m] as Mat4;
- },
-
- multiply(a: Mat4, b: Mat4): Mat4 {
- const out = new Array(16) as Mat4;
- for (let row = 0; row < 4; row++) {
- for (let col = 0; col < 4; col++) {
- out[col * 4 + row] =
- a[0 * 4 + row] * b[col * 4 + 0] +
- a[1 * 4 + row] * b[col * 4 + 1] +
- a[2 * 4 + row] * b[col * 4 + 2] +
- a[3 * 4 + row] * b[col * 4 + 3];
- }
- }
- return out;
- },
-
- translate(tx: number, ty: number, tz = 0): Mat4 {
- return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, tx, ty, tz, 1];
- },
-
- scale(sx: number, sy: number, sz = 1): Mat4 {
- return [sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, sz, 0, 0, 0, 0, 1];
- },
-
- rotateX(rad: number): Mat4 {
- const c = Math.cos(rad);
- const s = Math.sin(rad);
- return [1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1];
- },
-
- rotateY(rad: number): Mat4 {
- const c = Math.cos(rad);
- const s = Math.sin(rad);
- return [c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1];
- },
-
- rotateZ(rad: number): Mat4 {
- const c = Math.cos(rad);
- const s = Math.sin(rad);
- return [c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
- },
-
- rotateAxis(axis: Vec3, rad: number): Mat4 {
- const norm = Vector3Math.normalize(axis);
- const x = norm[0];
- const y = norm[1];
- const z = norm[2];
- const c = Math.cos(rad);
- const s = Math.sin(rad);
- const t = 1 - c;
-
- return [
- t * x * x + c,
- t * x * y + s * z,
- t * x * z - s * y,
- 0,
- t * x * y - s * z,
- t * y * y + c,
- t * y * z + s * x,
- 0,
- t * x * z + s * y,
- t * y * z - s * x,
- t * z * z + c,
- 0,
- 0,
- 0,
- 0,
- 1,
- ];
- },
-
- /**
- * Computes LookAt view matrix from camera eye, target, and up vector.
- */
- lookAt(eye: Vec3, target: Vec3, up: Vec3 = [0, -1, 0]): Mat4 {
- let f = Vector3Math.subtract(target, eye);
- if (Vector3Math.lengthSq(f) === 0) {
- f = [0, 0, 1];
- } else {
- f = Vector3Math.normalize(f);
- }
-
- let r = Vector3Math.cross(f, up);
- if (Vector3Math.lengthSq(r) === 0) {
- r = [1, 0, 0];
- } else {
- r = Vector3Math.normalize(r);
- }
-
- const u = Vector3Math.cross(r, f);
-
- return [
- r[0],
- u[0],
- f[0],
- 0,
- r[1],
- u[1],
- f[1],
- 0,
- r[2],
- u[2],
- f[2],
- 0,
- -Vector3Math.dot(r, eye),
- -Vector3Math.dot(u, eye),
- -Vector3Math.dot(f, eye),
- 1,
- ];
- },
-
- /**
- * Perspective projection matrix for WebGPU NDC depth range [0, 1].
- * @param fovYRad Vertical field of view in radians.
- * @param aspect Width / Height aspect ratio.
- * @param near Near clipping plane (e.g. 1.0).
- * @param far Far clipping plane (e.g. 10000.0).
- */
- perspective(
- fovYRad: number,
- aspect: number,
- near: number,
- far: number,
- ): Mat4 {
- const f = 1.0 / Math.tan(fovYRad / 2.0);
- const rangeInv = 1.0 / (far - near);
-
- return [
- f / aspect,
- 0,
- 0,
- 0,
- 0,
- f,
- 0,
- 0,
- 0,
- 0,
- far * rangeInv,
- 1, // W_clip = Z_eye
- 0,
- 0,
- -near * far * rangeInv,
- 0,
- ];
- },
-
- /**
- * Orthographic projection matrix for WebGPU NDC depth range [0, 1].
- */
- orthographic(
- left: number,
- right: number,
- bottom: number,
- top: number,
- near: number,
- far: number,
- ): Mat4 {
- const lr = 1.0 / (right - left);
- const bt = 1.0 / (top - bottom);
- const nf = 1.0 / (far - near);
-
- return [
- 2 * lr,
- 0,
- 0,
- 0,
- 0,
- 2 * bt,
- 0,
- 0,
- 0,
- 0,
- nf,
- 0,
- -(right + left) * lr,
- -(top + bottom) * bt,
- -near * nf,
- 1,
- ];
- },
-
- /**
- * Inverts a 4x4 matrix using standard Cramer's rule. Returns null if singular.
- */
- invert(m: Mat4): Mat4 | null {
- const inv = new Array(16) as Mat4;
-
- inv[0] =
- m[5] * m[10] * m[15] -
- m[5] * m[11] * m[14] -
- m[9] * m[6] * m[15] +
- m[9] * m[7] * m[14] +
- m[13] * m[6] * m[11] -
- m[13] * m[7] * m[10];
-
- inv[4] =
- -m[4] * m[10] * m[15] +
- m[4] * m[11] * m[14] +
- m[8] * m[6] * m[15] -
- m[8] * m[7] * m[14] -
- m[12] * m[6] * m[11] +
- m[12] * m[7] * m[10];
-
- inv[8] =
- m[4] * m[9] * m[15] -
- m[4] * m[11] * m[13] -
- m[8] * m[5] * m[15] +
- m[8] * m[7] * m[13] +
- m[12] * m[5] * m[11] -
- m[12] * m[7] * m[9];
-
- inv[12] =
- -m[4] * m[9] * m[14] +
- m[4] * m[10] * m[13] +
- m[8] * m[5] * m[14] -
- m[8] * m[6] * m[13] -
- m[12] * m[5] * m[10] +
- m[12] * m[6] * m[9];
-
- inv[1] =
- -m[1] * m[10] * m[15] +
- m[1] * m[11] * m[14] +
- m[9] * m[2] * m[15] -
- m[9] * m[3] * m[14] -
- m[13] * m[2] * m[11] +
- m[13] * m[3] * m[10];
-
- inv[5] =
- m[0] * m[10] * m[15] -
- m[0] * m[11] * m[14] -
- m[8] * m[2] * m[15] +
- m[8] * m[3] * m[14] +
- m[12] * m[2] * m[11] -
- m[12] * m[3] * m[10];
-
- inv[9] =
- -m[0] * m[9] * m[15] +
- m[0] * m[11] * m[13] +
- m[8] * m[1] * m[15] -
- m[8] * m[3] * m[13] -
- m[12] * m[1] * m[11] +
- m[12] * m[3] * m[9];
-
- inv[13] =
- m[0] * m[9] * m[14] -
- m[0] * m[10] * m[13] -
- m[8] * m[1] * m[14] +
- m[8] * m[2] * m[13] +
- m[12] * m[1] * m[10] -
- m[12] * m[2] * m[9];
-
- inv[2] =
- m[1] * m[6] * m[15] -
- m[1] * m[7] * m[14] -
- m[5] * m[2] * m[15] +
- m[5] * m[3] * m[14] +
- m[13] * m[2] * m[7] -
- m[13] * m[3] * m[6];
-
- inv[6] =
- -m[0] * m[6] * m[15] +
- m[0] * m[7] * m[14] +
- m[4] * m[2] * m[15] -
- m[4] * m[3] * m[14] -
- m[12] * m[2] * m[7] +
- m[12] * m[3] * m[6];
-
- inv[10] =
- m[0] * m[5] * m[15] -
- m[0] * m[7] * m[13] -
- m[4] * m[1] * m[15] +
- m[4] * m[3] * m[13] +
- m[12] * m[1] * m[7] -
- m[12] * m[3] * m[5];
-
- inv[14] =
- -m[0] * m[5] * m[14] +
- m[0] * m[6] * m[13] +
- m[4] * m[1] * m[14] -
- m[4] * m[2] * m[13] -
- m[12] * m[1] * m[6] +
- m[12] * m[2] * m[5];
-
- inv[3] =
- -m[1] * m[6] * m[11] +
- m[1] * m[7] * m[10] +
- m[5] * m[2] * m[11] -
- m[5] * m[3] * m[10] -
- m[9] * m[2] * m[7] +
- m[9] * m[3] * m[6];
-
- inv[7] =
- m[0] * m[6] * m[11] -
- m[0] * m[7] * m[10] -
- m[4] * m[2] * m[11] +
- m[4] * m[3] * m[10] +
- m[8] * m[2] * m[7] -
- m[8] * m[3] * m[6];
-
- inv[11] =
- -m[0] * m[5] * m[11] +
- m[0] * m[7] * m[9] +
- m[4] * m[1] * m[11] -
- m[4] * m[3] * m[9] -
- m[8] * m[1] * m[7] +
- m[8] * m[3] * m[5];
-
- inv[15] =
- m[0] * m[5] * m[10] -
- m[0] * m[6] * m[9] -
- m[4] * m[1] * m[10] +
- m[4] * m[2] * m[9] +
- m[8] * m[1] * m[6] -
- m[8] * m[2] * m[5];
-
- let det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
- if (Math.abs(det) < 1e-12) {
- return null;
- }
-
- det = 1.0 / det;
- for (let i = 0; i < 16; i++) {
- inv[i] *= det;
- }
-
- return inv;
- },
-
- transpose(m: Mat4): Mat4 {
- return [
- m[0],
- m[4],
- m[8],
- m[12],
- m[1],
- m[5],
- m[9],
- m[13],
- m[2],
- m[6],
- m[10],
- m[14],
- m[3],
- m[7],
- m[11],
- m[15],
- ];
- },
-
- /**
- * Projects a 3D point through a 4x4 matrix with perspective division.
- */
- projectPoint(m: Mat4, p: Vec3): Vec3 {
- const x = p[0];
- const y = p[1];
- const z = p[2];
-
- const X = m[0] * x + m[4] * y + m[8] * z + m[12];
- const Y = m[1] * x + m[5] * y + m[9] * z + m[13];
- const Z = m[2] * x + m[6] * y + m[10] * z + m[14];
- const W = m[3] * x + m[7] * y + m[11] * z + m[15];
-
- const wInv = Math.abs(W) > 1e-6 ? 1.0 / W : 1.0;
- return [X * wInv, Y * wInv, Z * wInv];
- },
-
- /**
- * Projects a 2D rectangle (0,0, w, h) through a 4x4 matrix into 4 3D points.
- */
- projectRectangle(
- m: Mat4,
- width: number,
- height: number,
- ): [Vec3, Vec3, Vec3, Vec3] {
- return [
- Matrix4Math.projectPoint(m, [0, 0, 0]),
- Matrix4Math.projectPoint(m, [width, 0, 0]),
- Matrix4Math.projectPoint(m, [width, height, 0]),
- Matrix4Math.projectPoint(m, [0, height, 0]),
- ];
- },
-
- toFloat32Array(m: Mat4): Float32Array {
- return new Float32Array(m);
- },
-};
diff --git a/packages/renderers/src/math3d/path3d.ts b/packages/renderers/src/math3d/path3d.ts
deleted file mode 100644
index df15bb83..00000000
--- a/packages/renderers/src/math3d/path3d.ts
+++ /dev/null
@@ -1,575 +0,0 @@
-/**
- * 3D Path Mathematical Curves, Arc-Length Lookup Tables, and Rotation-Minimizing Frames (RMF).
- * Provides uniform-velocity parameterization and smooth tangent/normal kinematics along 3D curves.
- */
-
-import { type Vec3, Vector3Math } from "./vector3.js";
-
-export interface Path3DPoint {
- /** Position in 3D world space [x, y, z] */
- position: Vec3;
- /** Normalized unit tangent forward vector */
- tangent: Vec3;
- /** Normalized unit normal vector (orthogonal to tangent) */
- normal: Vec3;
- /** Normalized unit binormal vector: cross(tangent, normal) */
- binormal: Vec3;
- /** Cumulative arc length distance in pixels from path origin */
- distance: number;
- /** Normalized progress along path in [0, 1] */
- t: number;
- /** Euler orientation angles in degrees [pitch, yaw, roll] */
- rotation: Vec3;
-}
-
-export interface Path3DSample {
- position: Vec3;
- tangent: Vec3;
- distance: number;
-}
-
-export interface CatmullRomOptions {
- /** Tension factor between 0 (loose) and 1 (tight); defaults to 0.5 (centripetal-like) */
- tension?: number;
- /** Whether path closes in a continuous loop */
- closed?: boolean;
- /** Samples per segment for arc-length integration (default 40) */
- samplesPerSegment?: number;
-}
-
-export interface BezierSegment3D {
- p0: Vec3;
- p1: Vec3;
- p2: Vec3;
- p3: Vec3;
-}
-
-export interface HelixOptions {
- center: Vec3;
- radius: number;
- pitch: number;
- turns: number;
- axis?: "x" | "y" | "z";
- samplesPerTurn?: number;
-}
-
-export interface CirclePathOptions {
- center: Vec3;
- radius: number;
- normal?: Vec3;
- startAngleDeg?: number;
- endAngleDeg?: number;
- samples?: number;
-}
-
-export class Path3D {
- private samples: Path3DSample[] = [];
- private frames: Array<{ normal: Vec3; binormal: Vec3; rotation: Vec3 }> = [];
- public totalLength = 0;
-
- constructor(samples: Path3DSample[]) {
- if (samples.length < 2) {
- throw new Error("Path3D requires at least 2 coordinate samples");
- }
- this.samples = samples;
- this.totalLength = samples[samples.length - 1].distance;
- this.computeRotationMinimizingFrames();
- }
-
- /**
- * Computes continuous Rotation-Minimizing Frames (RMF) along the path
- * using the Double Reflection Method (Wang et al. 2008), preventing gimbal flips.
- */
- private computeRotationMinimizingFrames(): void {
- const count = this.samples.length;
- this.frames = new Array(count);
-
- // Initial normal: pick arbitrary vector perpendicular to initial tangent T0
- const t0 = this.samples[0].tangent;
- let upRef: Vec3 = [0, -1, 0];
- if (Math.abs(Vector3Math.dot(t0, upRef)) > 0.95) {
- upRef = [0, 0, 1];
- }
- let n0 = Vector3Math.normalize(
- Vector3Math.subtract(
- upRef,
- Vector3Math.multiplyScalar(t0, Vector3Math.dot(upRef, t0)),
- ),
- );
- if (Vector3Math.lengthSq(n0) < 1e-4) {
- n0 = [1, 0, 0];
- }
- const b0 = Vector3Math.normalize(Vector3Math.cross(t0, n0));
- this.frames[0] = {
- normal: n0,
- binormal: b0,
- rotation: this.eulerFromFrame(t0, n0, b0),
- };
-
- // Double reflection across successive tangents
- for (let i = 0; i < count - 1; i++) {
- const x0 = this.samples[i].position;
- const x1 = this.samples[i + 1].position;
- const t_i = this.samples[i].tangent;
- const t_next = this.samples[i + 1].tangent;
- const n_i = this.frames[i].normal;
-
- const v1 = Vector3Math.subtract(x1, x0);
- const c1 = Vector3Math.dot(v1, v1);
-
- let n_next: Vec3;
- if (c1 > 1e-8) {
- // Reflection 1
- const t_i_L = Vector3Math.subtract(
- t_i,
- Vector3Math.multiplyScalar(v1, (2 * Vector3Math.dot(v1, t_i)) / c1),
- );
- const n_i_L = Vector3Math.subtract(
- n_i,
- Vector3Math.multiplyScalar(v1, (2 * Vector3Math.dot(v1, n_i)) / c1),
- );
-
- // Reflection 2
- const v2 = Vector3Math.subtract(t_next, t_i_L);
- const c2 = Vector3Math.dot(v2, v2);
- if (c2 > 1e-8) {
- n_next = Vector3Math.subtract(
- n_i_L,
- Vector3Math.multiplyScalar(
- v2,
- (2 * Vector3Math.dot(v2, n_i_L)) / c2,
- ),
- );
- } else {
- n_next = n_i_L;
- }
- } else {
- n_next = n_i;
- }
-
- n_next = Vector3Math.normalize(
- Vector3Math.subtract(
- n_next,
- Vector3Math.multiplyScalar(t_next, Vector3Math.dot(t_next, n_next)),
- ),
- );
- const b_next = Vector3Math.normalize(Vector3Math.cross(t_next, n_next));
-
- this.frames[i + 1] = {
- normal: n_next,
- binormal: b_next,
- rotation: this.eulerFromFrame(t_next, n_next, b_next),
- };
- }
- }
-
- /**
- * Extracts pitch, yaw, roll Euler angles in degrees from the orthonormal frame.
- */
- private eulerFromFrame(tangent: Vec3, normal: Vec3, binormal: Vec3): Vec3 {
- // Tangent is the forward viewing axis Z_fwd or X_fwd
- const pitch =
- Math.asin(Math.max(-1, Math.min(1, -tangent[1]))) * (180 / Math.PI);
- const yaw = Math.atan2(tangent[0], tangent[2]) * (180 / Math.PI);
- const roll = Math.atan2(normal[0], normal[1]) * (180 / Math.PI);
- return [pitch, yaw, roll];
- }
-
- /**
- * Evaluates point, tangent, normal, binormal, and rotation at normalized progress u in [0, 1].
- * Progression moves with constant velocity along the arc length of the path.
- */
- public getPointAt(u: number): Path3DPoint {
- const clampedU = Math.max(0, Math.min(1, u));
- const targetDist = clampedU * this.totalLength;
- return this.getPointAtDistance(targetDist);
- }
-
- /**
- * Evaluates point along path by absolute arc-length distance in pixels.
- */
- public getPointAtDistance(distance: number): Path3DPoint {
- const d = Math.max(0, Math.min(this.totalLength, distance));
- const count = this.samples.length;
-
- // Binary search in cumulative distance LUT
- let low = 0;
- let high = count - 1;
- while (low < high) {
- const mid = (low + high) >> 1;
- if (this.samples[mid].distance < d) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
-
- const idx1 = Math.max(1, low);
- const idx0 = idx1 - 1;
- const s0 = this.samples[idx0];
- const s1 = this.samples[idx1];
- const segLen = s1.distance - s0.distance;
- const alpha = segLen > 1e-6 ? (d - s0.distance) / segLen : 0;
-
- const position = Vector3Math.lerp(s0.position, s1.position, alpha);
- const tangent = Vector3Math.normalize(
- Vector3Math.lerp(s0.tangent, s1.tangent, alpha),
- );
-
- const f0 = this.frames[idx0];
- const f1 = this.frames[idx1];
- const normal = Vector3Math.normalize(
- Vector3Math.lerp(f0.normal, f1.normal, alpha),
- );
- const binormal = Vector3Math.normalize(Vector3Math.cross(tangent, normal));
- const rotation: Vec3 = [
- f0.rotation[0] + (f1.rotation[0] - f0.rotation[0]) * alpha,
- f0.rotation[1] + (f1.rotation[1] - f0.rotation[1]) * alpha,
- f0.rotation[2] + (f1.rotation[2] - f0.rotation[2]) * alpha,
- ];
-
- return {
- position,
- tangent,
- normal,
- binormal,
- distance: d,
- t: this.totalLength > 0 ? d / this.totalLength : 0,
- rotation,
- };
- }
-
- // =========================================================================
- // Path Factory Constructors
- // =========================================================================
-
- /**
- * Creates a smooth Catmull-Rom spline path interpolating through 3D waypoints.
- */
- public static catmullRom(
- waypoints: Vec3[],
- options: CatmullRomOptions = {},
- ): Path3D {
- if (waypoints.length < 2) {
- throw new Error("Catmull-Rom spline requires at least 2 waypoints");
- }
- const closed = Boolean(options.closed);
- const samplesPerSegment = options.samplesPerSegment ?? 40;
-
- // Build extended control point array
- const pts: Vec3[] = [];
- if (closed) {
- pts.push(waypoints[waypoints.length - 1]);
- pts.push(...waypoints);
- pts.push(waypoints[0]);
- pts.push(waypoints[1]);
- } else {
- // Extrapolate virtual endpoints for boundary tangents
- const pFirst = waypoints[0];
- const pSecond = waypoints[1];
- const pLast = waypoints[waypoints.length - 1];
- const pPrevLast = waypoints[waypoints.length - 2];
- pts.push(
- Vector3Math.subtract(pFirst, Vector3Math.subtract(pSecond, pFirst)),
- );
- pts.push(...waypoints);
- pts.push(Vector3Math.add(pLast, Vector3Math.subtract(pLast, pPrevLast)));
- }
-
- const numSegments = closed ? waypoints.length : waypoints.length - 1;
- const rawSamples: Array<{ position: Vec3; tangent: Vec3 }> = [];
-
- for (let s = 0; s < numSegments; s++) {
- const p0 = pts[s];
- const p1 = pts[s + 1];
- const p2 = pts[s + 2];
- const p3 = pts[s + 3];
-
- const steps =
- s === numSegments - 1 ? samplesPerSegment : samplesPerSegment - 1;
- for (let step = 0; step <= steps; step++) {
- const t = step / samplesPerSegment;
- const t2 = t * t;
- const t3 = t2 * t;
-
- // Standard Catmull-Rom polynomial basis
- const a0 = -0.5 * t3 + t2 - 0.5 * t;
- const a1 = 1.5 * t3 - 2.5 * t2 + 1.0;
- const a2 = -1.5 * t3 + 2.0 * t2 + 0.5 * t;
- const a3 = 0.5 * t3 - 0.5 * t2;
-
- const pos: Vec3 = [
- a0 * p0[0] + a1 * p1[0] + a2 * p2[0] + a3 * p3[0],
- a0 * p0[1] + a1 * p1[1] + a2 * p2[1] + a3 * p3[1],
- a0 * p0[2] + a1 * p1[2] + a2 * p2[2] + a3 * p3[2],
- ];
-
- // Velocity derivative for exact tangent
- const da0 = -1.5 * t2 + 2.0 * t - 0.5;
- const da1 = 4.5 * t2 - 5.0 * t;
- const da2 = -4.5 * t2 + 4.0 * t + 0.5;
- const da3 = 1.5 * t2 - 1.0 * t;
-
- const tanVel: Vec3 = [
- da0 * p0[0] + da1 * p1[0] + da2 * p2[0] + da3 * p3[0],
- da0 * p0[1] + da1 * p1[1] + da2 * p2[1] + da3 * p3[1],
- da0 * p0[2] + da1 * p1[2] + da2 * p2[2] + da3 * p3[2],
- ];
- const tangent = Vector3Math.normalize(tanVel);
-
- rawSamples.push({ position: pos, tangent });
- }
- }
-
- // Compute cumulative arc-length distances
- let cumulativeDist = 0;
- const samples: Path3DSample[] = [
- {
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- },
- ];
-
- for (let i = 1; i < rawSamples.length; i++) {
- const d = Vector3Math.distance(
- rawSamples[i].position,
- rawSamples[i - 1].position,
- );
- cumulativeDist += d;
- samples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
- distance: cumulativeDist,
- });
- }
-
- return new Path3D(samples);
- }
-
- /**
- * Creates a continuous 3D path from a chain of cubic Bezier segments.
- */
- public static bezier(
- segments: BezierSegment3D[],
- samplesPerSegment = 50,
- ): Path3D {
- if (segments.length === 0) {
- throw new Error("Bezier path requires at least 1 segment");
- }
- const rawSamples: Array<{ position: Vec3; tangent: Vec3 }> = [];
-
- for (let s = 0; s < segments.length; s++) {
- const seg = segments[s];
- const steps =
- s === segments.length - 1 ? samplesPerSegment : samplesPerSegment - 1;
-
- for (let step = 0; step <= steps; step++) {
- const t = step / samplesPerSegment;
- const u = 1 - t;
- const uu = u * u;
- const uuu = uu * u;
- const tt = t * t;
- const ttt = tt * t;
-
- const pos: Vec3 = [
- uuu * seg.p0[0] +
- 3 * uu * t * seg.p1[0] +
- 3 * u * tt * seg.p2[0] +
- ttt * seg.p3[0],
- uuu * seg.p0[1] +
- 3 * uu * t * seg.p1[1] +
- 3 * u * tt * seg.p2[1] +
- ttt * seg.p3[1],
- uuu * seg.p0[2] +
- 3 * uu * t * seg.p1[2] +
- 3 * u * tt * seg.p2[2] +
- ttt * seg.p3[2],
- ];
-
- const tanVel: Vec3 = [
- 3 * uu * (seg.p1[0] - seg.p0[0]) +
- 6 * u * t * (seg.p2[0] - seg.p1[0]) +
- 3 * tt * (seg.p3[0] - seg.p2[0]),
- 3 * uu * (seg.p1[1] - seg.p0[1]) +
- 6 * u * t * (seg.p2[1] - seg.p1[1]) +
- 3 * tt * (seg.p3[1] - seg.p2[1]),
- 3 * uu * (seg.p1[2] - seg.p0[2]) +
- 6 * u * t * (seg.p2[2] - seg.p1[2]) +
- 3 * tt * (seg.p3[2] - seg.p2[2]),
- ];
- const tangent = Vector3Math.normalize(tanVel);
- rawSamples.push({ position: pos, tangent });
- }
- }
-
- let cumulativeDist = 0;
- const samples: Path3DSample[] = [
- {
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- },
- ];
-
- for (let i = 1; i < rawSamples.length; i++) {
- cumulativeDist += Vector3Math.distance(
- rawSamples[i].position,
- rawSamples[i - 1].position,
- );
- samples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
- distance: cumulativeDist,
- });
- }
-
- return new Path3D(samples);
- }
-
- /**
- * Creates a 3D volumetric helix / spiral path.
- */
- public static helix(options: HelixOptions): Path3D {
- const {
- center,
- radius,
- pitch,
- turns,
- axis = "z",
- samplesPerTurn = 60,
- } = options;
- const totalSamples = Math.max(10, Math.round(turns * samplesPerTurn));
- const rawSamples: Array<{ position: Vec3; tangent: Vec3 }> = [];
-
- for (let i = 0; i <= totalSamples; i++) {
- const progress = i / totalSamples;
- const theta = progress * turns * Math.PI * 2;
- const axial = (progress - 0.5) * pitch * turns;
-
- const c = Math.cos(theta);
- const s = Math.sin(theta);
-
- let pos: Vec3;
- let tanVel: Vec3;
-
- if (axis === "z") {
- pos = [
- center[0] + radius * c,
- center[1] + radius * s,
- center[2] + axial,
- ];
- tanVel = [
- -radius * s,
- radius * c,
- (pitch * turns) / (turns * Math.PI * 2),
- ];
- } else if (axis === "y") {
- pos = [
- center[0] + radius * c,
- center[1] + axial,
- center[2] + radius * s,
- ];
- tanVel = [
- -radius * s,
- (pitch * turns) / (turns * Math.PI * 2),
- radius * c,
- ];
- } else {
- pos = [
- center[0] + axial,
- center[1] + radius * c,
- center[2] + radius * s,
- ];
- tanVel = [
- (pitch * turns) / (turns * Math.PI * 2),
- -radius * s,
- radius * c,
- ];
- }
-
- rawSamples.push({
- position: pos,
- tangent: Vector3Math.normalize(tanVel),
- });
- }
-
- let cumulativeDist = 0;
- const samples: Path3DSample[] = [
- {
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- },
- ];
-
- for (let i = 1; i < rawSamples.length; i++) {
- cumulativeDist += Vector3Math.distance(
- rawSamples[i].position,
- rawSamples[i - 1].position,
- );
- samples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
- distance: cumulativeDist,
- });
- }
-
- return new Path3D(samples);
- }
-
- /**
- * Creates a 3D circular or arc orbit path.
- */
- public static circle(options: CirclePathOptions): Path3D {
- const {
- center,
- radius,
- startAngleDeg = 0,
- endAngleDeg = 360,
- samples = 128,
- } = options;
- const startRad = (startAngleDeg * Math.PI) / 180;
- const endRad = (endAngleDeg * Math.PI) / 180;
- const rawSamples: Array<{ position: Vec3; tangent: Vec3 }> = [];
-
- for (let i = 0; i <= samples; i++) {
- const theta = startRad + (endRad - startRad) * (i / samples);
- const c = Math.cos(theta);
- const s = Math.sin(theta);
- const pos: Vec3 = [
- center[0] + radius * c,
- center[1],
- center[2] + radius * s,
- ];
- const tanVel: Vec3 = [-radius * s, 0, radius * c];
- rawSamples.push({
- position: pos,
- tangent: Vector3Math.normalize(tanVel),
- });
- }
-
- let cumulativeDist = 0;
- const pathSamples: Path3DSample[] = [
- {
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- },
- ];
-
- for (let i = 1; i < rawSamples.length; i++) {
- cumulativeDist += Vector3Math.distance(
- rawSamples[i].position,
- rawSamples[i - 1].position,
- );
- pathSamples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
- distance: cumulativeDist,
- });
- }
-
- return new Path3D(pathSamples);
- }
-}
diff --git a/packages/renderers/src/math3d/vector3.ts b/packages/renderers/src/math3d/vector3.ts
deleted file mode 100644
index 763896f4..00000000
--- a/packages/renderers/src/math3d/vector3.ts
+++ /dev/null
@@ -1,82 +0,0 @@
-/**
- * 3D Vector mathematics for camera kinematics, scene projections, and spatial transforms.
- * Fully typed, pure functional routines with zero runtime allocations where possible.
- */
-
-export type Vec3 = [number, number, number];
-
-export const Vector3Math = {
- create(x = 0, y = 0, z = 0): Vec3 {
- return [x, y, z];
- },
-
- copy(v: Vec3): Vec3 {
- return [v[0], v[1], v[2]];
- },
-
- set(out: Vec3, x: number, y: number, z: number): Vec3 {
- out[0] = x;
- out[1] = y;
- out[2] = z;
- return out;
- },
-
- add(a: Vec3, b: Vec3): Vec3 {
- return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
- },
-
- subtract(a: Vec3, b: Vec3): Vec3 {
- return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
- },
-
- multiplyScalar(v: Vec3, s: number): Vec3 {
- return [v[0] * s, v[1] * s, v[2] * s];
- },
-
- dot(a: Vec3, b: Vec3): number {
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
- },
-
- cross(a: Vec3, b: Vec3): Vec3 {
- return [
- a[1] * b[2] - a[2] * b[1],
- a[2] * b[0] - a[0] * b[2],
- a[0] * b[1] - a[1] * b[0],
- ];
- },
-
- lengthSq(v: Vec3): number {
- return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
- },
-
- length(v: Vec3): number {
- return Math.hypot(v[0], v[1], v[2]);
- },
-
- distance(a: Vec3, b: Vec3): number {
- return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
- },
-
- normalize(v: Vec3): Vec3 {
- const len = Math.hypot(v[0], v[1], v[2]);
- if (len === 0) return [0, 0, 0];
- const inv = 1 / len;
- return [v[0] * inv, v[1] * inv, v[2] * inv];
- },
-
- lerp(a: Vec3, b: Vec3, t: number): Vec3 {
- return [
- a[0] + (b[0] - a[0]) * t,
- a[1] + (b[1] - a[1]) * t,
- a[2] + (b[2] - a[2]) * t,
- ];
- },
-
- equals(a: Vec3, b: Vec3, epsilon = 1e-6): boolean {
- return (
- Math.abs(a[0] - b[0]) <= epsilon &&
- Math.abs(a[1] - b[1]) <= epsilon &&
- Math.abs(a[2] - b[2]) <= epsilon
- );
- },
-};
diff --git a/packages/webgpu-renderers/package.json b/packages/webgpu-renderers/package.json
index 51e55348..e745e929 100644
--- a/packages/webgpu-renderers/package.json
+++ b/packages/webgpu-renderers/package.json
@@ -16,6 +16,18 @@
"development": "./src/index.ts",
"import": "./dist/index.mjs",
"default": "./dist/index.mjs"
+ },
+ "./text-animations": {
+ "types": "./dist/text-animations/index.d.mts",
+ "development": "./src/text-animations/index.ts",
+ "import": "./dist/text-animations/index.mjs",
+ "default": "./dist/text-animations/index.mjs"
+ },
+ "./math3d": {
+ "types": "./dist/math3d/index.d.mts",
+ "development": "./src/math3d/index.ts",
+ "import": "./dist/math3d/index.mjs",
+ "default": "./dist/math3d/index.mjs"
}
},
"files": [
diff --git a/packages/webgpu-renderers/src/headless/dom-matrix.ts b/packages/webgpu-renderers/src/headless/dom-matrix.ts
index 418acd2a..d7f0064b 100644
--- a/packages/webgpu-renderers/src/headless/dom-matrix.ts
+++ b/packages/webgpu-renderers/src/headless/dom-matrix.ts
@@ -136,3 +136,35 @@ export class HeadlessDOMMatrix {
}
export type HeadlessDOMMatrixLike = HeadlessDOMMatrix;
+
+/** Installs DOMMatrix / DOMPoint where the runtime (Node) lacks them. */
+export function installDOMGeometryGlobals(): void {
+ const domObj = globalThis as unknown as { DOMMatrix?: any; DOMPoint?: any };
+ if (typeof domObj.DOMMatrix === "undefined") {
+ domObj.DOMMatrix = HeadlessDOMMatrix;
+ }
+
+ if (typeof domObj.DOMPoint === "undefined") {
+ domObj.DOMPoint = class DOMPoint {
+ constructor(
+ public x = 0,
+ public y = 0,
+ public z = 0,
+ public w = 1,
+ ) {}
+ static fromPoint(other: {
+ x?: number;
+ y?: number;
+ z?: number;
+ w?: number;
+ }) {
+ return new DOMPoint(
+ other.x ?? 0,
+ other.y ?? 0,
+ other.z ?? 0,
+ other.w ?? 1,
+ );
+ }
+ };
+ }
+}
diff --git a/packages/webgpu-renderers/src/headless/dom-shims.ts b/packages/webgpu-renderers/src/headless/dom-shims.ts
new file mode 100644
index 00000000..bee9e23c
--- /dev/null
+++ b/packages/webgpu-renderers/src/headless/dom-shims.ts
@@ -0,0 +1,112 @@
+/**
+ * Minimal DOM stand-ins for libraries (lottie) that expect a browser page when
+ * running headless: a 1920×1080 placeholder element, canvas element
+ * properties, and an inert IntersectionObserver.
+ */
+
+export function createHeadlessDOMElement() {
+ return {
+ clientWidth: 1920,
+ clientHeight: 1080,
+ getBoundingClientRect: () => ({
+ x: 0,
+ y: 0,
+ left: 0,
+ top: 0,
+ right: 1920,
+ bottom: 1080,
+ width: 1920,
+ height: 1080,
+ }),
+ style: {},
+ appendChild: () => {},
+ removeChild: () => {},
+ addEventListener: () => {},
+ removeEventListener: () => {},
+ getAttribute: () => null,
+ };
+}
+
+/** Gives a canvas class the element API lottie reads, where it's missing. */
+export function patchCanvasPrototypeForDOM(
+ proto: Record<string, unknown>,
+ parentElement: unknown,
+): void {
+ if (typeof proto.getBoundingClientRect === "undefined") {
+ proto.getBoundingClientRect = function (this: {
+ width?: number;
+ height?: number;
+ }) {
+ const width = this.width || 512;
+ const height = this.height || 512;
+ return {
+ x: 0,
+ y: 0,
+ left: 0,
+ top: 0,
+ right: width,
+ bottom: height,
+ width,
+ height,
+ };
+ };
+ }
+
+ if (!("style" in proto)) {
+ Object.defineProperty(proto, "style", {
+ get(this: { _style?: Record<string, unknown> }) {
+ this._style ??= {};
+ return this._style;
+ },
+ configurable: true,
+ });
+ }
+
+ if (!("clientWidth" in proto)) {
+ Object.defineProperty(proto, "clientWidth", {
+ get(this: { width?: number }) {
+ return this.width || 512;
+ },
+ configurable: true,
+ });
+ }
+
+ if (!("clientHeight" in proto)) {
+ Object.defineProperty(proto, "clientHeight", {
+ get(this: { height?: number }) {
+ return this.height || 512;
+ },
+ configurable: true,
+ });
+ }
+
+ if (!("parentElement" in proto)) {
+ Object.defineProperty(proto, "parentElement", {
+ get() {
+ return parentElement;
+ },
+ configurable: true,
+ });
+ }
+
+ if (typeof proto.addEventListener === "undefined") {
+ proto.addEventListener = () => {};
+ }
+ if (typeof proto.removeEventListener === "undefined") {
+ proto.removeEventListener = () => {};
+ }
+ if (typeof proto.getAttribute === "undefined") {
+ proto.getAttribute = () => null;
+ }
+}
+
+export function installIntersectionObserverStub(): void {
+ if (typeof globalThis.IntersectionObserver === "undefined") {
+ (globalThis as unknown as Record<string, unknown>).IntersectionObserver =
+ class IntersectionObserver {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ };
+ }
+}
diff --git a/packages/webgpu-renderers/src/headless/init.ts b/packages/webgpu-renderers/src/headless/init.ts
index d1aed145..fc32a43e 100644
--- a/packages/webgpu-renderers/src/headless/init.ts
+++ b/packages/webgpu-renderers/src/headless/init.ts
@@ -1,5 +1,5 @@
import { debugLog } from "../debug-log.js";
-import { HeadlessDOMMatrix } from "./dom-matrix.js";
+import { installDOMGeometryGlobals } from "./dom-matrix.js";
export async function initHeadlessWebGPU(): Promise<void> {
const { create, globals } = await import(/* webpackIgnore: true */ "webgpu");
@@ -46,34 +46,7 @@ export async function initHeadlessWebGPU(): Promise<void> {
}
}
- const domObj = globalThis as unknown as { DOMMatrix?: any; DOMPoint?: any };
- if (typeof domObj.DOMMatrix === "undefined") {
- domObj.DOMMatrix = HeadlessDOMMatrix;
- }
-
- if (typeof domObj.DOMPoint === "undefined") {
- domObj.DOMPoint = class DOMPoint {
- constructor(
- public x = 0,
- public y = 0,
- public z = 0,
- public w = 1,
- ) {}
- static fromPoint(other: {
- x?: number;
- y?: number;
- z?: number;
- w?: number;
- }) {
- return new DOMPoint(
- other.x ?? 0,
- other.y ?? 0,
- other.z ?? 0,
- other.w ?? 1,
- );
- }
- };
- }
+ installDOMGeometryGlobals();
if (typeof (globalThis as any).createImageBitmap === "undefined") {
(globalThis as any).createImageBitmap = async (source: any) => {
diff --git a/packages/webgpu-renderers/src/index.ts b/packages/webgpu-renderers/src/index.ts
index c9b1c530..453359c5 100644
--- a/packages/webgpu-renderers/src/index.ts
+++ b/packages/webgpu-renderers/src/index.ts
@@ -1,16 +1,22 @@
-export * from "./audio/webgpu-audio-processor.js";
export * from "./audio/audio-latent-engine.js";
+export * from "./audio/webgpu-audio-processor.js";
export * from "./color.js";
export * from "./device.js";
+export * from "./headless/dom-shims.js";
export * from "./headless/init.js";
export * from "./headless-font-loader.js";
export * from "./input-store.js";
+export * from "./loaders/index.js";
export * from "./lut-store.js";
+export * from "./math3d/index.js";
export * from "./nodes/index.js";
export * from "./render-context.js";
export * from "./renderer2d/buffer-pool.js";
+export * from "./renderer2d/effect-pipeline.js";
export * from "./renderer2d/index.js";
+export * from "./renderer2d/screen-space-relight-pipeline.js";
export * from "./renderer2d/text-atlas.js";
+export * from "./renderer3d/index.js";
export * from "./shader-store.js";
export * from "./shaders/audio-mesh-displace.js";
export * from "./shaders/color-matrix.js";
@@ -19,22 +25,13 @@ export * from "./shaders/lut-viewer.js";
export * from "./shaders/motion-blur.js";
export * from "./shaders/path.js";
export * from "./shaders/pbr-glass.js";
+export * from "./shaders/screen-space-relight.js";
export * from "./shaders/shape-fill.js";
export * from "./shaders/solid.js";
export * from "./shaders/ssao.js";
export * from "./signals/index.js";
export * from "./slug/index.js";
export * from "./surface-provider.js";
+export * from "./text-animations/index.js";
export * from "./texture-cache.js";
export * from "./utils.js";
-
-export * from "./math3d/index.js";
-export * from "./loaders/index.js";
-export * from "./renderer2d/effect-pipeline.js";
-export * from "./renderer3d/index.js";
-export * from "./shaders/screen-space-relight.js";
-export * from "./renderer2d/screen-space-relight-pipeline.js";
-export * from "./text-animations/index.js";
-
-
-
diff --git a/packages/webgpu-renderers/src/input-store.ts b/packages/webgpu-renderers/src/input-store.ts
index 7e2a3606..e5097aba 100644
--- a/packages/webgpu-renderers/src/input-store.ts
+++ b/packages/webgpu-renderers/src/input-store.ts
@@ -1,5 +1,26 @@
import { ALL_FORMATS, FilePathSource, Input, UrlSource } from "mediabunny";
+const isNodeRuntime = () =>
+ typeof process !== "undefined" && Boolean(process.versions?.node);
+
+/** In the browser, local paths are served through the dev server's `/@fs`. */
+function resolveInputUrl(url: string, isNode: boolean): string {
+ let resolvedUrl = url;
+ if (!isNode && typeof window !== "undefined") {
+ if (resolvedUrl.startsWith("file://")) {
+ resolvedUrl = resolvedUrl.slice(7);
+ }
+ if (
+ resolvedUrl.startsWith("/") &&
+ !resolvedUrl.startsWith("/@fs/") &&
+ !resolvedUrl.startsWith("/api/")
+ ) {
+ resolvedUrl = "/@fs" + resolvedUrl;
+ }
+ }
+ return resolvedUrl;
+}
+
class InputStore {
private store = new Map<
string,
@@ -8,21 +29,8 @@ class InputStore {
async acquire(url: string): Promise<Input> {
if (!url) throw new Error("Cannot acquire Input for empty URL");
- let resolvedUrl = url;
- const isNode = typeof process !== "undefined" && Boolean(process.versions?.node);
-
- if (!isNode && typeof window !== "undefined") {
- if (resolvedUrl.startsWith("file://")) {
- resolvedUrl = resolvedUrl.slice(7);
- }
- if (
- resolvedUrl.startsWith("/") &&
- !resolvedUrl.startsWith("/@fs/") &&
- !resolvedUrl.startsWith("/api/")
- ) {
- resolvedUrl = "/@fs" + resolvedUrl;
- }
- }
+ const isNode = isNodeRuntime();
+ const resolvedUrl = resolveInputUrl(url, isNode);
let shared = this.store.get(resolvedUrl);
if (!shared) {
@@ -57,21 +65,7 @@ class InputStore {
release(url: string): void {
if (!url) return;
- let resolvedUrl = url;
- const isNode = typeof process !== "undefined" && Boolean(process.versions?.node);
-
- if (!isNode && typeof window !== "undefined") {
- if (resolvedUrl.startsWith("file://")) {
- resolvedUrl = resolvedUrl.slice(7);
- }
- if (
- resolvedUrl.startsWith("/") &&
- !resolvedUrl.startsWith("/@fs/") &&
- !resolvedUrl.startsWith("/api/")
- ) {
- resolvedUrl = "/@fs" + resolvedUrl;
- }
- }
+ const resolvedUrl = resolveInputUrl(url, isNodeRuntime());
const shared = this.store.get(resolvedUrl);
if (!shared) return;
diff --git a/packages/webgpu-renderers/src/loaders/3ds-loader.ts b/packages/webgpu-renderers/src/loaders/3ds-loader.ts
index eb991e6f..4fc78af1 100644
--- a/packages/webgpu-renderers/src/loaders/3ds-loader.ts
+++ b/packages/webgpu-renderers/src/loaders/3ds-loader.ts
@@ -1,6 +1,6 @@
import fs from "node:fs";
-import { type Vec3, Vector3Math } from "../math3d/index.js";
import type { LoadModelOptions, Material3D, Mesh3DData, Model3DData } from "./types.js";
+import { accumulateVertexNormals, computeBounds, createIndexArray, resolveModelTransform, transformModelBounds, transformPositions } from "./mesh-utils.js";
// 3DS Chunk IDs
const CHUNK_MAIN = 0x4d4d;
@@ -236,111 +236,21 @@ export class ThreeDSLoader {
materials: Record<string, Material3D>,
options: LoadModelOptions,
): Model3DData {
- let minX = Infinity, minY = Infinity, minZ = Infinity;
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
-
- for (const m of rawMeshes) {
- for (let i = 0; i < m.positions.length; i += 3) {
- const x = m.positions[i]!;
- const y = m.positions[i + 1]!;
- const z = m.positions[i + 2]!;
- if (x < minX) minX = x;
- if (x > maxX) maxX = x;
- if (y < minY) minY = y;
- if (y > maxY) maxY = y;
- if (z < minZ) minZ = z;
- if (z > maxZ) maxZ = z;
- }
- }
-
- if (!Number.isFinite(minX)) {
- minX = minY = minZ = 0;
- maxX = maxY = maxZ = 0;
- }
-
- const centerX = (minX + maxX) * 0.5;
- const centerY = (minY + maxY) * 0.5;
- const centerZ = (minZ + maxZ) * 0.5;
-
- const sizeX = maxX - minX;
- const sizeY = maxY - minY;
- const sizeZ = maxZ - minZ;
- const maxDimension = Math.max(sizeX, sizeY, sizeZ, 0.0001);
-
- let scaleMultiplier = 1.0;
- if (options.normalizeSize && options.normalizeSize > 0) {
- scaleMultiplier = options.normalizeSize / maxDimension;
- }
-
- let scaleX = scaleMultiplier;
- let scaleY = scaleMultiplier;
- let scaleZ = scaleMultiplier;
- if (typeof options.scale === "number") {
- scaleX *= options.scale;
- scaleY *= options.scale;
- scaleZ *= options.scale;
- } else if (Array.isArray(options.scale)) {
- scaleX *= options.scale[0];
- scaleY *= options.scale[1];
- scaleZ *= options.scale[2];
- }
-
- const offsetX = options.center ? centerX : 0;
- const offsetY = options.center ? centerY : 0;
- const offsetZ = options.center ? centerZ : 0;
+ const bounds = computeBounds(...rawMeshes.map((m) => m.positions));
+ const transform = resolveModelTransform(bounds, options);
const meshes: Mesh3DData[] = rawMeshes.map((m, idx) => {
- const positions = new Float32Array(m.positions.length);
- for (let i = 0; i < m.positions.length; i += 3) {
- positions[i] = (m.positions[i]! - offsetX) * scaleX;
- positions[i + 1] = (m.positions[i + 1]! - offsetY) * scaleY;
- positions[i + 2] = (m.positions[i + 2]! - offsetZ) * scaleZ;
- }
+ const positions = transformPositions(m.positions, transform);
// Compute vertex normals
const normals = new Float32Array(m.positions.length);
- for (let i = 0; i < m.indices.length; i += 3) {
- const i0 = m.indices[i]!;
- const i1 = m.indices[i + 1]!;
- const i2 = m.indices[i + 2]!;
-
- const p0: Vec3 = [positions[i0 * 3]!, positions[i0 * 3 + 1]!, positions[i0 * 3 + 2]!];
- const p1: Vec3 = [positions[i1 * 3]!, positions[i1 * 3 + 1]!, positions[i1 * 3 + 2]!];
- const p2: Vec3 = [positions[i2 * 3]!, positions[i2 * 3 + 1]!, positions[i2 * 3 + 2]!];
-
- const e1 = Vector3Math.subtract(p1, p0);
- const e2 = Vector3Math.subtract(p2, p0);
- const fn = Vector3Math.cross(e1, e2);
-
- normals[i0 * 3] += fn[0];
- normals[i0 * 3 + 1] += fn[1];
- normals[i0 * 3 + 2] += fn[2];
-
- normals[i1 * 3] += fn[0];
- normals[i1 * 3 + 1] += fn[1];
- normals[i1 * 3 + 2] += fn[2];
-
- normals[i2 * 3] += fn[0];
- normals[i2 * 3 + 1] += fn[1];
- normals[i2 * 3 + 2] += fn[2];
- }
-
- for (let i = 0; i < normals.length; i += 3) {
- const nx = normals[i]!;
- const ny = normals[i + 1]!;
- const nz = normals[i + 2]!;
- const len = Math.hypot(nx, ny, nz) || 1.0;
- normals[i] = nx / len;
- normals[i + 1] = ny / len;
- normals[i + 2] = nz / len;
- }
+ accumulateVertexNormals(positions, m.indices, normals);
const uvs = m.uvs.length === (m.positions.length / 3) * 2
? new Float32Array(m.uvs)
: new Float32Array((m.positions.length / 3) * 2);
- const isLarge = positions.length / 3 > 65535;
- const indexArray = isLarge ? new Uint32Array(m.indices) : new Uint16Array(m.indices);
+ const indexArray = createIndexArray(m.indices, positions.length / 3);
return {
id: `3ds_mesh_${idx}`,
@@ -353,25 +263,13 @@ export class ThreeDSLoader {
};
});
- const finalMinX = (minX - offsetX) * scaleX;
- const finalMinY = (minY - offsetY) * scaleY;
- const finalMinZ = (minZ - offsetZ) * scaleZ;
- const finalMaxX = (maxX - offsetX) * scaleX;
- const finalMaxY = (maxY - offsetY) * scaleY;
- const finalMaxZ = (maxZ - offsetZ) * scaleZ;
return {
name: "3DS_Model",
meshes,
materials,
animations: [],
- bounds: {
- min: [finalMinX, finalMinY, finalMinZ],
- max: [finalMaxX, finalMaxY, finalMaxZ],
- center: [(finalMinX + finalMaxX) * 0.5, (finalMinY + finalMaxY) * 0.5, (finalMinZ + finalMaxZ) * 0.5],
- size: [finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ],
- boundingSphereRadius: Math.hypot(finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ) * 0.5,
- },
+ bounds: transformModelBounds(bounds, transform),
};
}
diff --git a/packages/webgpu-renderers/src/loaders/mesh-utils.ts b/packages/webgpu-renderers/src/loaders/mesh-utils.ts
new file mode 100644
index 00000000..b0e780a8
--- /dev/null
+++ b/packages/webgpu-renderers/src/loaders/mesh-utils.ts
@@ -0,0 +1,171 @@
+import { type Vec3, Vector3Math } from "../math3d/index.js";
+import type { LoadModelOptions } from "./types.js";
+
+/**
+ * Adds each triangle's (area-weighted) face normal to its three vertices, then
+ * normalizes every vertex normal in place.
+ */
+export function accumulateVertexNormals(
+ positions: ArrayLike<number>,
+ indices: ArrayLike<number>,
+ normals: { [i: number]: number; length: number },
+): void {
+ const vertex = (i: number): Vec3 => [
+ positions[i * 3]!,
+ positions[i * 3 + 1]!,
+ positions[i * 3 + 2]!,
+ ];
+ for (let i = 0; i < indices.length; i += 3) {
+ const i0 = indices[i]!;
+ const i1 = indices[i + 1]!;
+ const i2 = indices[i + 2]!;
+ const p0 = vertex(i0);
+ const fn = Vector3Math.cross(
+ Vector3Math.subtract(vertex(i1), p0),
+ Vector3Math.subtract(vertex(i2), p0),
+ );
+ for (const v of [i0, i1, i2]) {
+ normals[v * 3] += fn[0];
+ normals[v * 3 + 1] += fn[1];
+ normals[v * 3 + 2] += fn[2];
+ }
+ }
+
+ for (let i = 0; i < normals.length; i += 3) {
+ const nx = normals[i]!;
+ const ny = normals[i + 1]!;
+ const nz = normals[i + 2]!;
+ const len = Math.hypot(nx, ny, nz) || 1.0;
+ normals[i] = nx / len;
+ normals[i + 1] = ny / len;
+ normals[i + 2] = nz / len;
+ }
+}
+
+export interface MeshBounds {
+ min: Vec3;
+ max: Vec3;
+ center: Vec3;
+ size: Vec3;
+}
+
+/** Axis-aligned bounds of xyz position arrays (all zero when empty). */
+export function computeBounds(
+ ...positionArrays: ArrayLike<number>[]
+): MeshBounds {
+ let minX = Infinity,
+ minY = Infinity,
+ minZ = Infinity;
+ let maxX = -Infinity,
+ maxY = -Infinity,
+ maxZ = -Infinity;
+ for (const positions of positionArrays) {
+ for (let i = 0; i < positions.length; i += 3) {
+ const x = positions[i]!;
+ const y = positions[i + 1]!;
+ const z = positions[i + 2]!;
+ if (x < minX) minX = x;
+ if (x > maxX) maxX = x;
+ if (y < minY) minY = y;
+ if (y > maxY) maxY = y;
+ if (z < minZ) minZ = z;
+ if (z > maxZ) maxZ = z;
+ }
+ }
+ if (!Number.isFinite(minX)) {
+ minX = minY = minZ = 0;
+ maxX = maxY = maxZ = 0;
+ }
+ return {
+ min: [minX, minY, minZ],
+ max: [maxX, maxY, maxZ],
+ center: [(minX + maxX) * 0.5, (minY + maxY) * 0.5, (minZ + maxZ) * 0.5],
+ size: [maxX - minX, maxY - minY, maxZ - minZ],
+ };
+}
+
+export interface ModelTransform {
+ offset: Vec3;
+ scale: Vec3;
+}
+
+/**
+ * The placement `LoadModelOptions` asks for: recentring on the bounds
+ * (`center`), fitting the largest side to `normalizeSize`, then `scale`.
+ */
+export function resolveModelTransform(
+ bounds: MeshBounds,
+ options: LoadModelOptions,
+): ModelTransform {
+ const maxDimension = Math.max(...bounds.size, 0.0001);
+ let scaleMultiplier = 1.0;
+ if (options.normalizeSize && options.normalizeSize > 0) {
+ scaleMultiplier = options.normalizeSize / maxDimension;
+ }
+
+ const scale: Vec3 = [scaleMultiplier, scaleMultiplier, scaleMultiplier];
+ if (typeof options.scale === "number") {
+ scale[0] *= options.scale;
+ scale[1] *= options.scale;
+ scale[2] *= options.scale;
+ } else if (Array.isArray(options.scale)) {
+ scale[0] *= options.scale[0];
+ scale[1] *= options.scale[1];
+ scale[2] *= options.scale[2];
+ }
+
+ return { offset: options.center ? bounds.center : [0, 0, 0], scale };
+}
+
+/** `(p - offset) * scale` for every xyz position. */
+export function transformPositions(
+ positions: ArrayLike<number>,
+ { offset, scale }: ModelTransform,
+): Float32Array {
+ const out = new Float32Array(positions.length);
+ for (let i = 0; i < positions.length; i += 3) {
+ out[i] = (positions[i]! - offset[0]) * scale[0];
+ out[i + 1] = (positions[i + 1]! - offset[1]) * scale[1];
+ out[i + 2] = (positions[i + 2]! - offset[2]) * scale[2];
+ }
+ return out;
+}
+
+/** Uint16 indices while every vertex fits, Uint32 beyond 65535 vertices. */
+export function createIndexArray(
+ indices: number[],
+ vertexCount: number,
+): Uint16Array | Uint32Array {
+ return vertexCount > 65535
+ ? new Uint32Array(indices)
+ : new Uint16Array(indices);
+}
+
+/** The model's bounds after `transform`, as `Model3DData.bounds` reports them. */
+export function transformModelBounds(
+ bounds: MeshBounds,
+ { offset, scale }: ModelTransform,
+): MeshBounds & { boundingSphereRadius: number } {
+ const min: Vec3 = [
+ (bounds.min[0] - offset[0]) * scale[0],
+ (bounds.min[1] - offset[1]) * scale[1],
+ (bounds.min[2] - offset[2]) * scale[2],
+ ];
+ const max: Vec3 = [
+ (bounds.max[0] - offset[0]) * scale[0],
+ (bounds.max[1] - offset[1]) * scale[1],
+ (bounds.max[2] - offset[2]) * scale[2],
+ ];
+ return {
+ min,
+ max,
+ center: [
+ (min[0] + max[0]) * 0.5,
+ (min[1] + max[1]) * 0.5,
+ (min[2] + max[2]) * 0.5,
+ ],
+ size: [max[0] - min[0], max[1] - min[1], max[2] - min[2]],
+ boundingSphereRadius:
+ Math.hypot(max[0] - min[0], max[1] - min[1], max[2] - min[2]) * 0.5,
+ };
+}
diff --git a/packages/webgpu-renderers/src/loaders/off-loader.ts b/packages/webgpu-renderers/src/loaders/off-loader.ts
index 87b14143..8e3993df 100644
--- a/packages/webgpu-renderers/src/loaders/off-loader.ts
+++ b/packages/webgpu-renderers/src/loaders/off-loader.ts
@@ -1,6 +1,6 @@
import fs from "node:fs";
-import { type Vec3, Vector3Math } from "../math3d/index.js";
import type { LoadModelOptions, Material3D, Mesh3DData, Model3DData } from "./types.js";
+import { accumulateVertexNormals, computeBounds, createIndexArray, resolveModelTransform, transformModelBounds, transformPositions } from "./mesh-utils.js";
export class OffLoader {
/**
@@ -123,103 +123,14 @@ export class OffLoader {
// Calculate normals if missing
if (!hasNormals || options.smoothNormals) {
- for (let i = 0; i < rawIndices.length; i += 3) {
- const i0 = rawIndices[i]!;
- const i1 = rawIndices[i + 1]!;
- const i2 = rawIndices[i + 2]!;
-
- const p0: Vec3 = [rawPositions[i0 * 3]!, rawPositions[i0 * 3 + 1]!, rawPositions[i0 * 3 + 2]!];
- const p1: Vec3 = [rawPositions[i1 * 3]!, rawPositions[i1 * 3 + 1]!, rawPositions[i1 * 3 + 2]!];
- const p2: Vec3 = [rawPositions[i2 * 3]!, rawPositions[i2 * 3 + 1]!, rawPositions[i2 * 3 + 2]!];
-
- const e1 = Vector3Math.subtract(p1, p0);
- const e2 = Vector3Math.subtract(p2, p0);
- const fn = Vector3Math.cross(e1, e2);
-
- rawNormals[i0 * 3] += fn[0];
- rawNormals[i0 * 3 + 1] += fn[1];
- rawNormals[i0 * 3 + 2] += fn[2];
-
- rawNormals[i1 * 3] += fn[0];
- rawNormals[i1 * 3 + 1] += fn[1];
- rawNormals[i1 * 3 + 2] += fn[2];
-
- rawNormals[i2 * 3] += fn[0];
- rawNormals[i2 * 3 + 1] += fn[1];
- rawNormals[i2 * 3 + 2] += fn[2];
- }
-
- for (let i = 0; i < rawNormals.length; i += 3) {
- const nx = rawNormals[i]!;
- const ny = rawNormals[i + 1]!;
- const nz = rawNormals[i + 2]!;
- const len = Math.hypot(nx, ny, nz) || 1.0;
- rawNormals[i] = nx / len;
- rawNormals[i + 1] = ny / len;
- rawNormals[i + 2] = nz / len;
- }
- }
-
- let minX = Infinity, minY = Infinity, minZ = Infinity;
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
-
- for (let i = 0; i < rawPositions.length; i += 3) {
- const x = rawPositions[i]!;
- const y = rawPositions[i + 1]!;
- const z = rawPositions[i + 2]!;
- if (x < minX) minX = x;
- if (x > maxX) maxX = x;
- if (y < minY) minY = y;
- if (y > maxY) maxY = y;
- if (z < minZ) minZ = z;
- if (z > maxZ) maxZ = z;
+ accumulateVertexNormals(rawPositions, rawIndices, rawNormals);
}
- if (!Number.isFinite(minX)) {
- minX = minY = minZ = 0;
- maxX = maxY = maxZ = 0;
- }
-
- const centerX = (minX + maxX) * 0.5;
- const centerY = (minY + maxY) * 0.5;
- const centerZ = (minZ + maxZ) * 0.5;
-
- const sizeX = maxX - minX;
- const sizeY = maxY - minY;
- const sizeZ = maxZ - minZ;
- const maxDimension = Math.max(sizeX, sizeY, sizeZ, 0.0001);
+ const bounds = computeBounds(rawPositions);
+ const transform = resolveModelTransform(bounds, options);
+ const positions = transformPositions(rawPositions, transform);
- let scaleMultiplier = 1.0;
- if (options.normalizeSize && options.normalizeSize > 0) {
- scaleMultiplier = options.normalizeSize / maxDimension;
- }
-
- let scaleX = scaleMultiplier;
- let scaleY = scaleMultiplier;
- let scaleZ = scaleMultiplier;
- if (typeof options.scale === "number") {
- scaleX *= options.scale;
- scaleY *= options.scale;
- scaleZ *= options.scale;
- } else if (Array.isArray(options.scale)) {
- scaleX *= options.scale[0];
- scaleY *= options.scale[1];
- scaleZ *= options.scale[2];
- }
-
- const offsetX = options.center ? centerX : 0;
- const offsetY = options.center ? centerY : 0;
- const offsetZ = options.center ? centerZ : 0;
-
- const positions = new Float32Array(rawPositions.length);
- for (let i = 0; i < rawPositions.length; i += 3) {
- positions[i] = (rawPositions[i]! - offsetX) * scaleX;
- positions[i + 1] = (rawPositions[i + 1]! - offsetY) * scaleY;
- positions[i + 2] = (rawPositions[i + 2]! - offsetZ) * scaleZ;
- }
-
- const isLarge = positions.length / 3 > 65535;
- const indexArray = isLarge ? new Uint32Array(rawIndices) : new Uint16Array(rawIndices);
+ const indexArray = createIndexArray(rawIndices, positions.length / 3);
const defaultMaterial: Material3D = {
name: "off_default",
@@ -242,25 +153,13 @@ export class OffLoader {
},
];
- const finalMinX = (minX - offsetX) * scaleX;
- const finalMinY = (minY - offsetY) * scaleY;
- const finalMinZ = (minZ - offsetZ) * scaleZ;
- const finalMaxX = (maxX - offsetX) * scaleX;
- const finalMaxY = (maxY - offsetY) * scaleY;
- const finalMaxZ = (maxZ - offsetZ) * scaleZ;
return {
name: "OFF_Model",
meshes,
materials: { off_default: defaultMaterial },
animations: [],
- bounds: {
- min: [finalMinX, finalMinY, finalMinZ],
- max: [finalMaxX, finalMaxY, finalMaxZ],
- center: [(finalMinX + finalMaxX) * 0.5, (finalMinY + finalMaxY) * 0.5, (finalMinZ + finalMaxZ) * 0.5],
- size: [finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ],
- boundingSphereRadius: Math.hypot(finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ) * 0.5,
- },
+ bounds: transformModelBounds(bounds, transform),
};
}
diff --git a/packages/webgpu-renderers/src/loaders/ply-loader.ts b/packages/webgpu-renderers/src/loaders/ply-loader.ts
index dab54d7a..9145a85f 100644
--- a/packages/webgpu-renderers/src/loaders/ply-loader.ts
+++ b/packages/webgpu-renderers/src/loaders/ply-loader.ts
@@ -1,6 +1,6 @@
import fs from "node:fs";
-import { type Vec3, Vector3Math } from "../math3d/index.js";
import type { LoadModelOptions, Material3D, Mesh3DData, Model3DData } from "./types.js";
+import { accumulateVertexNormals, computeBounds, createIndexArray, resolveModelTransform, transformModelBounds, transformPositions } from "./mesh-utils.js";
interface PlyProperty {
name: string;
@@ -376,103 +376,14 @@ export class PlyLoader {
): Model3DData {
// Calculate face normals if missing
if (!hasNormals || options.smoothNormals) {
- for (let i = 0; i < rawIndices.length; i += 3) {
- const i0 = rawIndices[i]!;
- const i1 = rawIndices[i + 1]!;
- const i2 = rawIndices[i + 2]!;
-
- const p0: Vec3 = [rawPositions[i0 * 3]!, rawPositions[i0 * 3 + 1]!, rawPositions[i0 * 3 + 2]!];
- const p1: Vec3 = [rawPositions[i1 * 3]!, rawPositions[i1 * 3 + 1]!, rawPositions[i1 * 3 + 2]!];
- const p2: Vec3 = [rawPositions[i2 * 3]!, rawPositions[i2 * 3 + 1]!, rawPositions[i2 * 3 + 2]!];
-
- const e1 = Vector3Math.subtract(p1, p0);
- const e2 = Vector3Math.subtract(p2, p0);
- const fn = Vector3Math.cross(e1, e2);
-
- rawNormals[i0 * 3] += fn[0];
- rawNormals[i0 * 3 + 1] += fn[1];
- rawNormals[i0 * 3 + 2] += fn[2];
-
- rawNormals[i1 * 3] += fn[0];
- rawNormals[i1 * 3 + 1] += fn[1];
- rawNormals[i1 * 3 + 2] += fn[2];
-
- rawNormals[i2 * 3] += fn[0];
- rawNormals[i2 * 3 + 1] += fn[1];
- rawNormals[i2 * 3 + 2] += fn[2];
- }
-
- for (let i = 0; i < rawNormals.length; i += 3) {
- const nx = rawNormals[i]!;
- const ny = rawNormals[i + 1]!;
- const nz = rawNormals[i + 2]!;
- const len = Math.hypot(nx, ny, nz) || 1.0;
- rawNormals[i] = nx / len;
- rawNormals[i + 1] = ny / len;
- rawNormals[i + 2] = nz / len;
- }
- }
-
- let minX = Infinity, minY = Infinity, minZ = Infinity;
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
-
- for (let i = 0; i < rawPositions.length; i += 3) {
- const x = rawPositions[i]!;
- const y = rawPositions[i + 1]!;
- const z = rawPositions[i + 2]!;
- if (x < minX) minX = x;
- if (x > maxX) maxX = x;
- if (y < minY) minY = y;
- if (y > maxY) maxY = y;
- if (z < minZ) minZ = z;
- if (z > maxZ) maxZ = z;
- }
-
- if (!Number.isFinite(minX)) {
- minX = minY = minZ = 0;
- maxX = maxY = maxZ = 0;
- }
-
- const centerX = (minX + maxX) * 0.5;
- const centerY = (minY + maxY) * 0.5;
- const centerZ = (minZ + maxZ) * 0.5;
-
- const sizeX = maxX - minX;
- const sizeY = maxY - minY;
- const sizeZ = maxZ - minZ;
- const maxDimension = Math.max(sizeX, sizeY, sizeZ, 0.0001);
-
- let scaleMultiplier = 1.0;
- if (options.normalizeSize && options.normalizeSize > 0) {
- scaleMultiplier = options.normalizeSize / maxDimension;
- }
-
- let scaleX = scaleMultiplier;
- let scaleY = scaleMultiplier;
- let scaleZ = scaleMultiplier;
- if (typeof options.scale === "number") {
- scaleX *= options.scale;
- scaleY *= options.scale;
- scaleZ *= options.scale;
- } else if (Array.isArray(options.scale)) {
- scaleX *= options.scale[0];
- scaleY *= options.scale[1];
- scaleZ *= options.scale[2];
+ accumulateVertexNormals(rawPositions, rawIndices, rawNormals);
}
- const offsetX = options.center ? centerX : 0;
- const offsetY = options.center ? centerY : 0;
- const offsetZ = options.center ? centerZ : 0;
-
- const positions = new Float32Array(rawPositions.length);
- for (let i = 0; i < rawPositions.length; i += 3) {
- positions[i] = (rawPositions[i]! - offsetX) * scaleX;
- positions[i + 1] = (rawPositions[i + 1]! - offsetY) * scaleY;
- positions[i + 2] = (rawPositions[i + 2]! - offsetZ) * scaleZ;
- }
+ const bounds = computeBounds(rawPositions);
+ const transform = resolveModelTransform(bounds, options);
+ const positions = transformPositions(rawPositions, transform);
- const isLarge = positions.length / 3 > 65535;
- const indexArray = isLarge ? new Uint32Array(rawIndices) : new Uint16Array(rawIndices);
+ const indexArray = createIndexArray(rawIndices, positions.length / 3);
const defaultMaterial: Material3D = {
name: "ply_default",
@@ -495,25 +406,13 @@ export class PlyLoader {
},
];
- const finalMinX = (minX - offsetX) * scaleX;
- const finalMinY = (minY - offsetY) * scaleY;
- const finalMinZ = (minZ - offsetZ) * scaleZ;
- const finalMaxX = (maxX - offsetX) * scaleX;
- const finalMaxY = (maxY - offsetY) * scaleY;
- const finalMaxZ = (maxZ - offsetZ) * scaleZ;
return {
name: "PLY_Model",
meshes,
materials: { ply_default: defaultMaterial },
animations: [],
- bounds: {
- min: [finalMinX, finalMinY, finalMinZ],
- max: [finalMaxX, finalMaxY, finalMaxZ],
- center: [(finalMinX + finalMaxX) * 0.5, (finalMinY + finalMaxY) * 0.5, (finalMinZ + finalMaxZ) * 0.5],
- size: [finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ],
- boundingSphereRadius: Math.hypot(finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ) * 0.5,
- },
+ bounds: transformModelBounds(bounds, transform),
};
}
diff --git a/packages/webgpu-renderers/src/loaders/stl-loader.ts b/packages/webgpu-renderers/src/loaders/stl-loader.ts
index 36410e0a..4647f1b4 100644
--- a/packages/webgpu-renderers/src/loaders/stl-loader.ts
+++ b/packages/webgpu-renderers/src/loaders/stl-loader.ts
@@ -1,6 +1,7 @@
import fs from "node:fs";
import { type Vec3, Vector3Math } from "../math3d/index.js";
import type { LoadModelOptions, Material3D, Mesh3DData, Model3DData } from "./types.js";
+import { computeBounds, createIndexArray, resolveModelTransform, transformModelBounds, transformPositions } from "./mesh-utils.js";
export class StlLoader {
/**
@@ -192,80 +193,21 @@ export class StlLoader {
rawIndices: number[],
options: LoadModelOptions = {},
): Model3DData {
- let minX = Infinity;
- let minY = Infinity;
- let minZ = Infinity;
- let maxX = -Infinity;
- let maxY = -Infinity;
- let maxZ = -Infinity;
+ const bounds = computeBounds(rawPositions);
+ const transform = resolveModelTransform(bounds, options);
+ const positions = transformPositions(rawPositions, transform);
- for (let i = 0; i < rawPositions.length; i += 3) {
- const x = rawPositions[i]!;
- const y = rawPositions[i + 1]!;
- const z = rawPositions[i + 2]!;
- if (x < minX) minX = x;
- if (x > maxX) maxX = x;
- if (y < minY) minY = y;
- if (y > maxY) maxY = y;
- if (z < minZ) minZ = z;
- if (z > maxZ) maxZ = z;
- }
-
- if (!Number.isFinite(minX)) {
- minX = minY = minZ = 0;
- maxX = maxY = maxZ = 0;
- }
-
- const centerX = (minX + maxX) * 0.5;
- const centerY = (minY + maxY) * 0.5;
- const centerZ = (minZ + maxZ) * 0.5;
-
- const sizeX = maxX - minX;
- const sizeY = maxY - minY;
- const sizeZ = maxZ - minZ;
- const maxDimension = Math.max(sizeX, sizeY, sizeZ, 0.0001);
-
- let scaleMultiplier = 1.0;
- if (options.normalizeSize && options.normalizeSize > 0) {
- scaleMultiplier = options.normalizeSize / maxDimension;
- }
-
- let scaleX = scaleMultiplier;
- let scaleY = scaleMultiplier;
- let scaleZ = scaleMultiplier;
- if (typeof options.scale === "number") {
- scaleX *= options.scale;
- scaleY *= options.scale;
- scaleZ *= options.scale;
- } else if (Array.isArray(options.scale)) {
- scaleX *= options.scale[0];
- scaleY *= options.scale[1];
- scaleZ *= options.scale[2];
- }
-
- const offsetX = options.center ? centerX : 0;
- const offsetY = options.center ? centerY : 0;
- const offsetZ = options.center ? centerZ : 0;
-
- const positions = new Float32Array(rawPositions.length);
+ // Planar UV fallback based on bounding size
+ const [minX, minY] = bounds.min;
+ const [sizeX, sizeY] = bounds.size;
const uvs = new Float32Array((rawPositions.length / 3) * 2);
-
for (let i = 0; i < rawPositions.length; i += 3) {
- const px = (rawPositions[i]! - offsetX) * scaleX;
- const py = (rawPositions[i + 1]! - offsetY) * scaleY;
- const pz = (rawPositions[i + 2]! - offsetZ) * scaleZ;
- positions[i] = px;
- positions[i + 1] = py;
- positions[i + 2] = pz;
-
- // Planar UV fallback based on bounding size
const uvIdx = (i / 3) * 2;
uvs[uvIdx] = sizeX > 0 ? (rawPositions[i]! - minX) / sizeX : 0;
uvs[uvIdx + 1] = sizeY > 0 ? (rawPositions[i + 1]! - minY) / sizeY : 0;
}
- const isLarge = positions.length / 3 > 65535;
- const indexArray = isLarge ? new Uint32Array(rawIndices) : new Uint16Array(rawIndices);
+ const indexArray = createIndexArray(rawIndices, positions.length / 3);
const defaultMaterial: Material3D = {
name: "stl_default",
@@ -287,25 +229,13 @@ export class StlLoader {
},
];
- const finalMinX = (minX - offsetX) * scaleX;
- const finalMinY = (minY - offsetY) * scaleY;
- const finalMinZ = (minZ - offsetZ) * scaleZ;
- const finalMaxX = (maxX - offsetX) * scaleX;
- const finalMaxY = (maxY - offsetY) * scaleY;
- const finalMaxZ = (maxZ - offsetZ) * scaleZ;
return {
name: "STL_Model",
meshes,
materials: { stl_default: defaultMaterial },
animations: [],
- bounds: {
- min: [finalMinX, finalMinY, finalMinZ],
- max: [finalMaxX, finalMaxY, finalMaxZ],
- center: [(finalMinX + finalMaxX) * 0.5, (finalMinY + finalMaxY) * 0.5, (finalMinZ + finalMaxZ) * 0.5],
- size: [finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ],
- boundingSphereRadius: Math.hypot(finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ) * 0.5,
- },
+ bounds: transformModelBounds(bounds, transform),
};
}
diff --git a/packages/webgpu-renderers/src/loaders/vox-loader.ts b/packages/webgpu-renderers/src/loaders/vox-loader.ts
index 47e360db..1d943c0e 100644
--- a/packages/webgpu-renderers/src/loaders/vox-loader.ts
+++ b/packages/webgpu-renderers/src/loaders/vox-loader.ts
@@ -1,5 +1,6 @@
import fs from "node:fs";
import type { LoadModelOptions, Material3D, Mesh3DData, Model3DData } from "./types.js";
+import { computeBounds, createIndexArray, resolveModelTransform, transformModelBounds, transformPositions } from "./mesh-utils.js";
const DEFAULT_PALETTE: Array<[number, number, number, number]> = [
[0, 0, 0, 0],
@@ -207,66 +208,11 @@ export class VoxLoader {
}
}
- let minX = Infinity, minY = Infinity, minZ = Infinity;
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
-
- for (let i = 0; i < rawPositions.length; i += 3) {
- const x = rawPositions[i]!;
- const y = rawPositions[i + 1]!;
- const z = rawPositions[i + 2]!;
- if (x < minX) minX = x;
- if (x > maxX) maxX = x;
- if (y < minY) minY = y;
- if (y > maxY) maxY = y;
- if (z < minZ) minZ = z;
- if (z > maxZ) maxZ = z;
- }
-
- if (!Number.isFinite(minX)) {
- minX = minY = minZ = 0;
- maxX = maxY = maxZ = 0;
- }
-
- const centerX = (minX + maxX) * 0.5;
- const centerY = (minY + maxY) * 0.5;
- const centerZ = (minZ + maxZ) * 0.5;
+ const bounds = computeBounds(rawPositions);
+ const transform = resolveModelTransform(bounds, options);
+ const positions = transformPositions(rawPositions, transform);
- const sizeBoxX = maxX - minX;
- const sizeBoxY = maxY - minY;
- const sizeBoxZ = maxZ - minZ;
- const maxDimension = Math.max(sizeBoxX, sizeBoxY, sizeBoxZ, 0.0001);
-
- let scaleMultiplier = 1.0;
- if (options.normalizeSize && options.normalizeSize > 0) {
- scaleMultiplier = options.normalizeSize / maxDimension;
- }
-
- let scaleX = scaleMultiplier;
- let scaleY = scaleMultiplier;
- let scaleZ = scaleMultiplier;
- if (typeof options.scale === "number") {
- scaleX *= options.scale;
- scaleY *= options.scale;
- scaleZ *= options.scale;
- } else if (Array.isArray(options.scale)) {
- scaleX *= options.scale[0];
- scaleY *= options.scale[1];
- scaleZ *= options.scale[2];
- }
-
- const offsetX = options.center ? centerX : 0;
- const offsetY = options.center ? centerY : 0;
- const offsetZ = options.center ? centerZ : 0;
-
- const positions = new Float32Array(rawPositions.length);
- for (let i = 0; i < rawPositions.length; i += 3) {
- positions[i] = (rawPositions[i]! - offsetX) * scaleX;
- positions[i + 1] = (rawPositions[i + 1]! - offsetY) * scaleY;
- positions[i + 2] = (rawPositions[i + 2]! - offsetZ) * scaleZ;
- }
-
- const isLarge = positions.length / 3 > 65535;
- const indexArray = isLarge ? new Uint32Array(rawIndices) : new Uint16Array(rawIndices);
+ const indexArray = createIndexArray(rawIndices, positions.length / 3);
const defaultMaterial: Material3D = {
name: "vox_default",
@@ -289,25 +235,13 @@ export class VoxLoader {
},
];
- const finalMinX = (minX - offsetX) * scaleX;
- const finalMinY = (minY - offsetY) * scaleY;
- const finalMinZ = (minZ - offsetZ) * scaleZ;
- const finalMaxX = (maxX - offsetX) * scaleX;
- const finalMaxY = (maxY - offsetY) * scaleY;
- const finalMaxZ = (maxZ - offsetZ) * scaleZ;
return {
name: "VOX_Model",
meshes,
materials: { vox_default: defaultMaterial },
animations: [],
- bounds: {
- min: [finalMinX, finalMinY, finalMinZ],
- max: [finalMaxX, finalMaxY, finalMaxZ],
- center: [(finalMinX + finalMaxX) * 0.5, (finalMinY + finalMaxY) * 0.5, (finalMinZ + finalMaxZ) * 0.5],
- size: [finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ],
- boundingSphereRadius: Math.hypot(finalMaxX - finalMinX, finalMaxY - finalMinY, finalMaxZ - finalMinZ) * 0.5,
- },
+ bounds: transformModelBounds(bounds, transform),
};
}
diff --git a/packages/renderers/src/math3d/camera3d.test.ts b/packages/webgpu-renderers/src/math3d/camera3d.test.ts
similarity index 100%
rename from packages/renderers/src/math3d/camera3d.test.ts
rename to packages/webgpu-renderers/src/math3d/camera3d.test.ts
diff --git a/packages/webgpu-renderers/src/math3d/path3d.ts b/packages/webgpu-renderers/src/math3d/path3d.ts
index e2534f4b..c4e60707 100644
--- a/packages/webgpu-renderers/src/math3d/path3d.ts
+++ b/packages/webgpu-renderers/src/math3d/path3d.ts
@@ -307,24 +307,28 @@ export class Path3D {
}
}
- // Compute cumulative arc-length distances
+ return Path3D.fromRawSamples(rawSamples);
+ }
+
+ /** Samples annotated with their cumulative arc length along the path. */
+ private static fromRawSamples(
+ rawSamples: Array<{ position: Vec3; tangent: Vec3 }>,
+ ): Path3D {
let cumulativeDist = 0;
- const samples: Path3DSample[] = [{
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- }];
-
- for (let i = 1; i < rawSamples.length; i++) {
- const d = Vector3Math.distance(rawSamples[i].position, rawSamples[i - 1].position);
- cumulativeDist += d;
- samples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
+ const samples: Path3DSample[] = rawSamples.map((sample, i) => {
+ if (i > 0) {
+ cumulativeDist += Vector3Math.distance(
+ sample.position,
+ rawSamples[i - 1].position,
+ );
+ }
+ return {
+ position: sample.position,
+ tangent: sample.tangent,
distance: cumulativeDist,
- });
- }
-
+ };
+ });
+ if (samples.length === 0) throw new TypeError("Path3D needs samples");
return new Path3D(samples);
}
@@ -368,23 +372,7 @@ export class Path3D {
}
}
- let cumulativeDist = 0;
- const samples: Path3DSample[] = [{
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- }];
-
- for (let i = 1; i < rawSamples.length; i++) {
- cumulativeDist += Vector3Math.distance(rawSamples[i].position, rawSamples[i - 1].position);
- samples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
- distance: cumulativeDist,
- });
- }
-
- return new Path3D(samples);
+ return Path3D.fromRawSamples(rawSamples);
}
/**
@@ -420,23 +408,7 @@ export class Path3D {
rawSamples.push({ position: pos, tangent: Vector3Math.normalize(tanVel) });
}
- let cumulativeDist = 0;
- const samples: Path3DSample[] = [{
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- }];
-
- for (let i = 1; i < rawSamples.length; i++) {
- cumulativeDist += Vector3Math.distance(rawSamples[i].position, rawSamples[i - 1].position);
- samples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
- distance: cumulativeDist,
- });
- }
-
- return new Path3D(samples);
+ return Path3D.fromRawSamples(rawSamples);
}
/**
@@ -457,22 +429,6 @@ export class Path3D {
rawSamples.push({ position: pos, tangent: Vector3Math.normalize(tanVel) });
}
- let cumulativeDist = 0;
- const pathSamples: Path3DSample[] = [{
- position: rawSamples[0].position,
- tangent: rawSamples[0].tangent,
- distance: 0,
- }];
-
- for (let i = 1; i < rawSamples.length; i++) {
- cumulativeDist += Vector3Math.distance(rawSamples[i].position, rawSamples[i - 1].position);
- pathSamples.push({
- position: rawSamples[i].position,
- tangent: rawSamples[i].tangent,
- distance: cumulativeDist,
- });
- }
-
- return new Path3D(pathSamples);
+ return Path3D.fromRawSamples(rawSamples);
}
}
diff --git a/packages/webgpu-renderers/src/nodes/image-texture.ts b/packages/webgpu-renderers/src/nodes/image-texture.ts
new file mode 100644
index 00000000..2c647eae
--- /dev/null
+++ b/packages/webgpu-renderers/src/nodes/image-texture.ts
@@ -0,0 +1,38 @@
+/** A decoded image: a browser bitmap or, headless, raw RGBA8 pixels. */
+export interface DecodedImageData {
+ width: number;
+ height: number;
+ bitmap?: GPUCopyExternalImageSource | null;
+ buffer?: Parameters<GPUQueue["writeTexture"]>[1] | null;
+}
+
+/** An rgba8unorm texture holding `data` (left empty when it has neither form). */
+export function createImageTexture(
+ device: GPUDevice,
+ data: DecodedImageData,
+): GPUTexture {
+ const tex = device.createTexture({
+ size: [data.width, data.height],
+ format: "rgba8unorm",
+ usage:
+ GPUTextureUsage.TEXTURE_BINDING |
+ GPUTextureUsage.COPY_DST |
+ GPUTextureUsage.RENDER_ATTACHMENT,
+ });
+
+ if (data.bitmap) {
+ device.queue.copyExternalImageToTexture(
+ { source: data.bitmap },
+ { texture: tex },
+ [data.width, data.height],
+ );
+ } else if (data.buffer) {
+ device.queue.writeTexture(
+ { texture: tex },
+ data.buffer,
+ { bytesPerRow: data.width * 4 },
+ [data.width, data.height],
+ );
+ }
+ return tex;
+}
diff --git a/packages/webgpu-renderers/src/nodes/image.ts b/packages/webgpu-renderers/src/nodes/image.ts
index 3c7e670d..9fcb84e5 100644
--- a/packages/webgpu-renderers/src/nodes/image.ts
+++ b/packages/webgpu-renderers/src/nodes/image.ts
@@ -1,5 +1,6 @@
import type { RenderContextValue } from "../render-context.js";
import { textureCache } from "../texture-cache.js";
+import { createImageTexture } from "./image-texture.js";
import type { ImageNodeProps } from "./types.js";
interface LoadedImageData {
@@ -95,29 +96,7 @@ export async function drawImageNode(
// Check the cache again in case another concurrent render task already created and cached the texture!
tex = textureCache.acquire(props.src);
if (!tex) {
- tex = ctx.device.createTexture({
- size: [data.width, data.height],
- format: "rgba8unorm",
- usage:
- GPUTextureUsage.TEXTURE_BINDING |
- GPUTextureUsage.COPY_DST |
- GPUTextureUsage.RENDER_ATTACHMENT,
- });
-
- if (data.bitmap) {
- ctx.device.queue.copyExternalImageToTexture(
- { source: data.bitmap },
- { texture: tex },
- [data.width, data.height],
- );
- } else if (data.buffer) {
- ctx.device.queue.writeTexture(
- { texture: tex },
- data.buffer,
- { bytesPerRow: data.width * 4 },
- [data.width, data.height],
- );
- }
+ tex = createImageTexture(ctx.device, data);
textureCache.set(props.src, tex, ctx.device);
}
diff --git a/packages/webgpu-renderers/src/nodes/lottie.ts b/packages/webgpu-renderers/src/nodes/lottie.ts
index 7ac5b50e..b04ee628 100644
--- a/packages/webgpu-renderers/src/nodes/lottie.ts
+++ b/packages/webgpu-renderers/src/nodes/lottie.ts
@@ -19,6 +19,11 @@ function resolveUrl(url: string): string {
}
import { debugLog } from "../debug-log.js";
+import {
+ createHeadlessDOMElement,
+ installIntersectionObserverStub,
+ patchCanvasPrototypeForDOM,
+} from "../headless/dom-shims.js";
import type { RenderContextValue } from "../render-context.js";
import { textureCache } from "../texture-cache.js";
import type { LottieNodeProps } from "./types.js";
@@ -297,34 +302,9 @@ let wasmConfigured = false;
function polyfillHeadlessDOM(): void {
const globalObj = globalThis as unknown as Record<string, unknown>;
- const dummyElement = {
- clientWidth: 1920,
- clientHeight: 1080,
- getBoundingClientRect: () => ({
- x: 0,
- y: 0,
- left: 0,
- top: 0,
- right: 1920,
- bottom: 1080,
- width: 1920,
- height: 1080,
- }),
- style: {},
- appendChild: () => {},
- removeChild: () => {},
- addEventListener: () => {},
- removeEventListener: () => {},
- getAttribute: () => null,
- };
-
- if (typeof globalThis.IntersectionObserver === "undefined") {
- globalObj.IntersectionObserver = class IntersectionObserver {
- observe() {}
- unobserve() {}
- disconnect() {}
- };
- }
+ const dummyElement = createHeadlessDOMElement();
+
+ installIntersectionObserverStub();
if (typeof globalThis.HTMLCanvasElement === "undefined") {
if (typeof globalThis.OffscreenCanvas !== "undefined") {
@@ -355,74 +335,7 @@ function polyfillHeadlessDOM(): void {
const proto = (
CanvasCtor as unknown as { prototype: Record<string, unknown> }
).prototype;
- if (proto) {
- if (typeof proto.getBoundingClientRect === "undefined") {
- proto.getBoundingClientRect = function (this: {
- width?: number;
- height?: number;
- }) {
- const width = this.width || 512;
- const height = this.height || 512;
- return {
- x: 0,
- y: 0,
- left: 0,
- top: 0,
- right: width,
- bottom: height,
- width,
- height,
- };
- };
- }
-
- if (!("style" in proto)) {
- Object.defineProperty(proto, "style", {
- get(this: { _style?: Record<string, unknown> }) {
- this._style ??= {};
- return this._style;
- },
- configurable: true,
- });
- }
-
- if (!("clientWidth" in proto)) {
- Object.defineProperty(proto, "clientWidth", {
- get(this: { width?: number }) {
- return this.width || 512;
- },
- configurable: true,
- });
- }
-
- if (!("clientHeight" in proto)) {
- Object.defineProperty(proto, "clientHeight", {
- get(this: { height?: number }) {
- return this.height || 512;
- },
- configurable: true,
- });
- }
-
- if (!("parentElement" in proto)) {
- Object.defineProperty(proto, "parentElement", {
- get() {
- return dummyElement;
- },
- configurable: true,
- });
- }
-
- if (typeof proto.addEventListener === "undefined") {
- proto.addEventListener = () => {};
- }
- if (typeof proto.removeEventListener === "undefined") {
- proto.removeEventListener = () => {};
- }
- if (typeof proto.getAttribute === "undefined") {
- proto.getAttribute = () => null;
- }
- }
+ if (proto) patchCanvasPrototypeForDOM(proto, dummyElement);
}
}
diff --git a/packages/webgpu-renderers/src/nodes/svg.ts b/packages/webgpu-renderers/src/nodes/svg.ts
index 67657744..16654988 100644
--- a/packages/webgpu-renderers/src/nodes/svg.ts
+++ b/packages/webgpu-renderers/src/nodes/svg.ts
@@ -1,5 +1,6 @@
import type { RenderContextValue } from "../render-context.js";
import { textureCache } from "../texture-cache.js";
+import { createImageTexture } from "./image-texture.js";
import type { SVGNodeProps } from "./types.js";
interface SvgData {
@@ -118,29 +119,7 @@ export async function drawSvgNode(
// Check the cache again in case another concurrent render task already created and cached the texture!
tex = textureCache.acquire(cacheKey);
if (!tex) {
- tex = ctx.device.createTexture({
- size: [data.width, data.height],
- format: "rgba8unorm",
- usage:
- GPUTextureUsage.TEXTURE_BINDING |
- GPUTextureUsage.COPY_DST |
- GPUTextureUsage.RENDER_ATTACHMENT,
- });
-
- if (data.bitmap) {
- ctx.device.queue.copyExternalImageToTexture(
- { source: data.bitmap },
- { texture: tex },
- [data.width, data.height],
- );
- } else if (data.buffer) {
- ctx.device.queue.writeTexture(
- { texture: tex },
- data.buffer,
- { bytesPerRow: data.width * 4 },
- [data.width, data.height],
- );
- }
+ tex = createImageTexture(ctx.device, data);
textureCache.set(cacheKey, tex, ctx.device);
}
diff --git a/packages/webgpu-renderers/src/renderer2d/motion-graphics.test.ts b/packages/webgpu-renderers/src/renderer2d/motion-graphics.test.ts
index 320bebf7..7f116ecd 100644
--- a/packages/webgpu-renderers/src/renderer2d/motion-graphics.test.ts
+++ b/packages/webgpu-renderers/src/renderer2d/motion-graphics.test.ts
@@ -5,6 +5,7 @@ import {
createMockDevice,
createMockTexture,
ensureDOMGlobals,
+ installMockGPUFlags,
} from "./test-helpers.js";
describe("Motion Graphics Pipeline", () => {
@@ -17,21 +18,7 @@ describe("Motion Graphics Pipeline", () => {
beforeEach(() => {
mockDevice = createMockDevice();
- globalThis.GPUBufferUsage = {
- UNIFORM: 1,
- VERTEX: 2,
- COPY_DST: 4,
- } as any;
- globalThis.GPUShaderStage = {
- VERTEX: 1,
- FRAGMENT: 2,
- } as any;
- globalThis.GPUTextureUsage = {
- TEXTURE_BINDING: 1,
- COPY_DST: 2,
- RENDER_ATTACHMENT: 4,
- COPY_SRC: 8,
- } as any;
+ installMockGPUFlags();
renderer = new Renderer2D(mockDevice, "rgba8unorm");
});
@@ -47,17 +34,11 @@ describe("Motion Graphics Pipeline", () => {
600,
);
- renderer.drawPath(
- pass,
- "M 0 0 L 100 0 L 100 100",
- "#ffffff",
- 4,
- {
- trimStart: 0,
- trimEnd: 0.5,
- strokeLineCap: "round",
- },
- );
+ renderer.drawPath(pass, "M 0 0 L 100 0 L 100 100", "#ffffff", 4, {
+ trimStart: 0,
+ trimEnd: 0.5,
+ strokeLineCap: "round",
+ });
expect(pass.draw).toHaveBeenCalled();
});
@@ -72,26 +53,14 @@ describe("Motion Graphics Pipeline", () => {
600,
);
- renderer.drawPath(
- pass,
- "M 10 10 L 50 50",
- "#ff0000",
- 6,
- {
- strokeLineCap: "butt",
- },
- );
+ renderer.drawPath(pass, "M 10 10 L 50 50", "#ff0000", 6, {
+ strokeLineCap: "butt",
+ });
expect(pass.draw).toHaveBeenCalled();
- renderer.drawPath(
- pass,
- "M 10 10 L 50 50",
- "#00ff00",
- 6,
- {
- strokeLineCap: "square",
- },
- );
+ renderer.drawPath(pass, "M 10 10 L 50 50", "#00ff00", 6, {
+ strokeLineCap: "square",
+ });
expect(pass.draw).toHaveBeenCalled();
});
});
diff --git a/packages/webgpu-renderers/src/renderer2d/path-pipeline.ts b/packages/webgpu-renderers/src/renderer2d/path-pipeline.ts
index 8d29496d..f183fd2f 100644
--- a/packages/webgpu-renderers/src/renderer2d/path-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer2d/path-pipeline.ts
@@ -4,13 +4,14 @@ import { BufferPool } from "./buffer-pool.js";
import type { Color, DrawOpts } from "./index.js";
import {
dashPathSegments,
- parseSvgPathToSegments,
type PathSegment,
type Point,
+ parseSvgPathToSegments,
trimPathSegments,
} from "./path-geometry.js";
-import { UniformBindGroupCache } from "./uniform-bind-group-cache.js";
+import { createUniformLayout, createVec2Pipeline } from "./pipeline-utils.js";
import type { TransformStack } from "./transform-stack.js";
+import { UniformBindGroupCache } from "./uniform-bind-group-cache.js";
export interface PathDrawOpts extends DrawOpts {
trimStart?: number;
@@ -47,92 +48,34 @@ export class PathPipeline {
code: pathWgsl,
});
- this.uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ this.uniformLayout = createUniformLayout(device);
- this.pipeline = device.createRenderPipeline({
+ this.pipeline = createVec2Pipeline(device, {
label: "PathPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [this.uniformLayout],
- }),
- vertex: {
- module,
- entryPoint: "vs",
- buffers: [
- {
- arrayStride: 8,
- attributes: [{ shaderLocation: 0, offset: 0, format: "float32x2" }],
- },
- ],
- },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ uniformLayout: this.uniformLayout,
+ format,
+ topology: "triangle-strip",
});
- this.maxPipeline = device.createRenderPipeline({
+ this.maxPipeline = createVec2Pipeline(device, {
label: "PathPipeline.max",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [this.uniformLayout],
- }),
- vertex: {
- module,
- entryPoint: "vs",
- buffers: [
- {
- arrayStride: 8,
- attributes: [{ shaderLocation: 0, offset: 0, format: "float32x2" }],
- },
- ],
- },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one",
- operation: "max",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one",
- operation: "max",
- },
- },
- },
- ],
+ module,
+ uniformLayout: this.uniformLayout,
+ format,
+ topology: "triangle-strip",
+ blend: {
+ color: {
+ srcFactor: "one",
+ dstFactor: "one",
+ operation: "max",
+ },
+ alpha: {
+ srcFactor: "one",
+ dstFactor: "one",
+ operation: "max",
+ },
},
- primitive: { topology: "triangle-strip" },
});
}
@@ -339,4 +282,3 @@ export class PathPipeline {
this.uniformBindGroupCache.destroy();
}
}
-
diff --git a/packages/webgpu-renderers/src/renderer2d/pipeline-utils.ts b/packages/webgpu-renderers/src/renderer2d/pipeline-utils.ts
new file mode 100644
index 00000000..53c7e0c8
--- /dev/null
+++ b/packages/webgpu-renderers/src/renderer2d/pipeline-utils.ts
@@ -0,0 +1,68 @@
+/** Premultiplied-alpha "over". */
+export const PREMULTIPLIED_OVER: GPUBlendState = {
+ color: {
+ srcFactor: "one",
+ dstFactor: "one-minus-src-alpha",
+ operation: "add",
+ },
+ alpha: {
+ srcFactor: "one",
+ dstFactor: "one-minus-src-alpha",
+ operation: "add",
+ },
+};
+
+/** One uniform buffer at binding 0, visible to both shader stages. */
+export function createUniformLayout(device: GPUDevice): GPUBindGroupLayout {
+ return device.createBindGroupLayout({
+ entries: [
+ {
+ binding: 0,
+ visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
+ buffer: { type: "uniform" },
+ },
+ ],
+ });
+}
+
+/**
+ * A pipeline drawing float32x2 vertices (`vs`/`fs`) with one uniform bind
+ * group into a single `format` target.
+ */
+export function createVec2Pipeline(
+ device: GPUDevice,
+ options: {
+ label: string;
+ module: GPUShaderModule;
+ uniformLayout: GPUBindGroupLayout;
+ format: GPUTextureFormat;
+ topology: GPUPrimitiveTopology;
+ blend?: GPUBlendState;
+ },
+): GPURenderPipeline {
+ const { module } = options;
+ return device.createRenderPipeline({
+ label: options.label,
+ layout: device.createPipelineLayout({
+ bindGroupLayouts: [options.uniformLayout],
+ }),
+ vertex: {
+ module,
+ entryPoint: "vs",
+ buffers: [
+ {
+ arrayStride: 8,
+ attributes: [{ shaderLocation: 0, offset: 0, format: "float32x2" }],
+ },
+ ],
+ },
+ fragment: {
+ module,
+ entryPoint: "fs",
+ targets: [
+ { format: options.format, blend: options.blend ?? PREMULTIPLIED_OVER },
+ ],
+ },
+ primitive: { topology: options.topology },
+ });
+}
diff --git a/packages/webgpu-renderers/src/renderer2d/quad-pipeline.ts b/packages/webgpu-renderers/src/renderer2d/quad-pipeline.ts
index 922d1fa9..b1871e5f 100644
--- a/packages/webgpu-renderers/src/renderer2d/quad-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer2d/quad-pipeline.ts
@@ -1,8 +1,9 @@
import { quadWgsl } from "../shaders/quad.js";
import type { BindGroupCache } from "./bind-group-cache.js";
import type { DrawOpts, Rect } from "./index.js";
-import { UniformBindGroupCache } from "./uniform-bind-group-cache.js";
+import { createUniformLayout, PREMULTIPLIED_OVER } from "./pipeline-utils.js";
import type { TransformStack } from "./transform-stack.js";
+import { UniformBindGroupCache } from "./uniform-bind-group-cache.js";
class BufferPool {
private buffers: GPUBuffer[] = [];
@@ -67,15 +68,7 @@ export class QuadPipeline {
code: quadWgsl,
});
- this.uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ this.uniformLayout = createUniformLayout(device);
this.textureLayout = device.createBindGroupLayout({
entries: [
@@ -118,18 +111,7 @@ export class QuadPipeline {
targets: [
{
format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
+ blend: PREMULTIPLIED_OVER,
},
],
},
diff --git a/packages/webgpu-renderers/src/renderer2d/renderer2d.test.ts b/packages/webgpu-renderers/src/renderer2d/renderer2d.test.ts
index f84320ee..028fed64 100644
--- a/packages/webgpu-renderers/src/renderer2d/renderer2d.test.ts
+++ b/packages/webgpu-renderers/src/renderer2d/renderer2d.test.ts
@@ -6,6 +6,7 @@ import {
createMockRenderPassEncoder,
createMockTexture,
ensureDOMGlobals,
+ installMockGPUFlags,
} from "./test-helpers.js";
describe("Renderer2D", () => {
@@ -18,21 +19,7 @@ describe("Renderer2D", () => {
beforeEach(() => {
mockDevice = createMockDevice();
- globalThis.GPUBufferUsage = {
- UNIFORM: 1,
- VERTEX: 2,
- COPY_DST: 4,
- } as any;
- globalThis.GPUShaderStage = {
- VERTEX: 1,
- FRAGMENT: 2,
- } as any;
- globalThis.GPUTextureUsage = {
- TEXTURE_BINDING: 1,
- COPY_DST: 2,
- RENDER_ATTACHMENT: 4,
- COPY_SRC: 8,
- } as any;
+ installMockGPUFlags();
renderer = new Renderer2D(mockDevice, "rgba8unorm");
});
diff --git a/packages/webgpu-renderers/src/renderer2d/screen-space-relight-pipeline.ts b/packages/webgpu-renderers/src/renderer2d/screen-space-relight-pipeline.ts
index e25a651c..52a63e82 100644
--- a/packages/webgpu-renderers/src/renderer2d/screen-space-relight-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer2d/screen-space-relight-pipeline.ts
@@ -7,6 +7,7 @@
import { screenSpaceRelightWgsl } from "../shaders/screen-space-relight.js";
import { BufferPool } from "./buffer-pool.js";
+import { PREMULTIPLIED_OVER } from "./pipeline-utils.js";
export interface ScreenSpaceRelightExecuteOpts {
viewportWidth: number;
@@ -124,18 +125,7 @@ export class ScreenSpaceRelightPipeline {
targets: [
{
format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
+ blend: PREMULTIPLIED_OVER,
},
],
},
@@ -155,10 +145,8 @@ export class ScreenSpaceRelightPipeline {
});
// Flat normal pointing along Z+: (0, 0, 1) -> encoded as (128, 128, 255, 255)
const flatNormals = new Uint8Array([
- 128, 128, 255, 255,
- 128, 128, 255, 255,
- 128, 128, 255, 255,
- 128, 128, 255, 255,
+ 128, 128, 255, 255, 128, 128, 255, 255, 128, 128, 255, 255, 128, 128,
+ 255, 255,
]);
this.device.queue.writeTexture(
{ texture: this.defaultFlatNormalTex },
diff --git a/packages/webgpu-renderers/src/renderer2d/shape-fill-pipeline.ts b/packages/webgpu-renderers/src/renderer2d/shape-fill-pipeline.ts
index 80248dc4..ac458716 100644
--- a/packages/webgpu-renderers/src/renderer2d/shape-fill-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer2d/shape-fill-pipeline.ts
@@ -5,6 +5,7 @@ import { shapeFillWgsl } from "../shaders/shape-fill.js";
import { BufferPool } from "./buffer-pool.js";
import { asGradient, packGradientUniforms } from "./gradient-uniforms.js";
import type { Color, DrawOpts, Rect } from "./index.js";
+import { createUniformLayout, createVec2Pipeline } from "./pipeline-utils.js";
import type { TransformStack } from "./transform-stack.js";
export type ShapeFillType = "solid" | "linear" | "radial" | "none";
@@ -47,53 +48,14 @@ export class ShapeFillPipeline {
code: shapeFillWgsl,
});
- this.uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ this.uniformLayout = createUniformLayout(device);
- this.pipeline = device.createRenderPipeline({
+ this.pipeline = createVec2Pipeline(device, {
label: "ShapeFillPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [this.uniformLayout],
- }),
- vertex: {
- module,
- entryPoint: "vs",
- buffers: [
- {
- arrayStride: 8,
- attributes: [{ shaderLocation: 0, offset: 0, format: "float32x2" }],
- },
- ],
- },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: { topology: "triangle-list" },
+ module,
+ uniformLayout: this.uniformLayout,
+ format,
+ topology: "triangle-list",
});
}
diff --git a/packages/webgpu-renderers/src/renderer2d/solid-pipeline.ts b/packages/webgpu-renderers/src/renderer2d/solid-pipeline.ts
index 68a70ac1..757b35ba 100644
--- a/packages/webgpu-renderers/src/renderer2d/solid-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer2d/solid-pipeline.ts
@@ -4,6 +4,7 @@ import { solidWgsl } from "../shaders/solid.js";
import { BufferPool } from "./buffer-pool.js";
import { asGradient, packGradientUniforms } from "./gradient-uniforms.js";
import type { Color, DrawOpts, Rect } from "./index.js";
+import { createUniformLayout, createVec2Pipeline } from "./pipeline-utils.js";
import type { TransformStack } from "./transform-stack.js";
import { UniformBindGroupCache } from "./uniform-bind-group-cache.js";
@@ -42,53 +43,14 @@ export class SolidPipeline {
code: solidWgsl,
});
- this.uniformLayout = device.createBindGroupLayout({
- entries: [
- {
- binding: 0,
- visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
- buffer: { type: "uniform" },
- },
- ],
- });
+ this.uniformLayout = createUniformLayout(device);
- this.pipeline = device.createRenderPipeline({
+ this.pipeline = createVec2Pipeline(device, {
label: "SolidPipeline",
- layout: device.createPipelineLayout({
- bindGroupLayouts: [this.uniformLayout],
- }),
- vertex: {
- module,
- entryPoint: "vs",
- buffers: [
- {
- arrayStride: 8,
- attributes: [{ shaderLocation: 0, offset: 0, format: "float32x2" }],
- },
- ],
- },
- fragment: {
- module,
- entryPoint: "fs",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- primitive: { topology: "triangle-strip" },
+ module,
+ uniformLayout: this.uniformLayout,
+ format,
+ topology: "triangle-strip",
});
}
diff --git a/packages/webgpu-renderers/src/renderer2d/test-helpers.ts b/packages/webgpu-renderers/src/renderer2d/test-helpers.ts
index 8af1ec31..c76a64c7 100644
--- a/packages/webgpu-renderers/src/renderer2d/test-helpers.ts
+++ b/packages/webgpu-renderers/src/renderer2d/test-helpers.ts
@@ -1,4 +1,5 @@
import { vi } from "vitest";
+import { installDOMGeometryGlobals } from "../headless/dom-matrix.js";
export interface MockTexture {
width: number;
@@ -47,7 +48,6 @@ export function createMockBuffer(size: number, usage: number): MockBuffer {
};
}
-
export function createMockDevice(): GPUDevice {
const onSubmittedWorkDonePromise = Promise.resolve();
const queue = {
@@ -68,17 +68,15 @@ export function createMockDevice(): GPUDevice {
createBuffer: vi
.fn()
.mockImplementation((desc) => createMockBuffer(desc.size, desc.usage)),
- createTexture: vi
- .fn()
- .mockImplementation((desc) => {
- const w = Array.isArray(desc.size)
- ? desc.size[0]
- : (desc.size?.width ?? 1024);
- const h = Array.isArray(desc.size)
- ? desc.size[1]
- : (desc.size?.height ?? 1024);
- return createMockTexture(w, h, desc.format, desc.usage);
- }),
+ createTexture: vi.fn().mockImplementation((desc) => {
+ const w = Array.isArray(desc.size)
+ ? desc.size[0]
+ : (desc.size?.width ?? 1024);
+ const h = Array.isArray(desc.size)
+ ? desc.size[1]
+ : (desc.size?.height ?? 1024);
+ return createMockTexture(w, h, desc.format, desc.usage);
+ }),
createSampler: vi.fn().mockImplementation(() => ({})),
createShaderModule: vi.fn().mockImplementation(() => ({})),
createBindGroupLayout: vi.fn().mockImplementation(() => ({})),
@@ -126,116 +124,24 @@ export function createMockCommandEncoder(): GPUCommandEncoder {
}
export function ensureDOMGlobals() {
- const domObj = globalThis as unknown as { DOMMatrix?: any; DOMPoint?: any };
- if (typeof domObj.DOMMatrix === "undefined") {
- domObj.DOMMatrix = class DOMMatrix {
- a = 1;
- b = 0;
- c = 0;
- d = 1;
- e = 0;
- f = 0;
-
- constructor(init?: any) {
- this.a = 1;
- this.b = 0;
- this.c = 0;
- this.d = 1;
- this.e = 0;
- this.f = 0;
- if (init instanceof DOMMatrix) {
- this.a = init.a;
- this.b = init.b;
- this.c = init.c;
- this.d = init.d;
- this.e = init.e;
- this.f = init.f;
- }
- }
-
- multiply(other: DOMMatrix): DOMMatrix {
- const m = new DOMMatrix();
- m.a = this.a * other.a + this.c * other.b;
- m.b = this.b * other.a + this.d * other.b;
- m.c = this.a * other.c + this.c * other.d;
- m.d = this.b * other.c + this.d * other.d;
- m.e = this.a * other.e + this.c * other.f + this.e;
- m.f = this.b * other.e + this.d * other.f + this.f;
- return m;
- }
-
- inverse(): DOMMatrix {
- const det = this.a * this.d - this.b * this.c;
- if (det === 0) throw new Error("Matrix is not invertible");
- const m = new DOMMatrix();
- m.a = this.d / det;
- m.b = -this.b / det;
- m.c = -this.c / det;
- m.d = this.a / det;
- m.e = (this.c * this.f - this.d * this.e) / det;
- m.f = (this.b * this.e - this.a * this.f) / det;
- return m;
- }
-
- transformPoint(point: { x: number; y: number }): {
- x: number;
- y: number;
- } {
- return {
- x: point.x * this.a + point.y * this.c + this.e,
- y: point.x * this.b + point.y * this.d + this.f,
- };
- }
-
- translate(x: number, y: number): DOMMatrix {
- const m = new DOMMatrix();
- m.e = x;
- m.f = y;
- return this.multiply(m);
- }
-
- scale(sx: number, sy?: number): DOMMatrix {
- const m = new DOMMatrix();
- m.a = sx;
- m.d = sy ?? sx;
- return this.multiply(m);
- }
-
- rotate(angle: number): DOMMatrix {
- const rad = (angle * Math.PI) / 180;
- const cos = Math.cos(rad);
- const sin = Math.sin(rad);
- const m = new DOMMatrix();
- m.a = cos;
- m.b = sin;
- m.c = -sin;
- m.d = cos;
- return this.multiply(m);
- }
- };
- }
+ installDOMGeometryGlobals();
+}
- if (typeof domObj.DOMPoint === "undefined") {
- domObj.DOMPoint = class DOMPoint {
- constructor(
- public x = 0,
- public y = 0,
- public z = 0,
- public w = 1,
- ) {}
- static fromPoint(other: {
- x?: number;
- y?: number;
- z?: number;
- w?: number;
- }) {
- return new DOMPoint(
- other.x ?? 0,
- other.y ?? 0,
- other.z ?? 0,
- other.w ?? 1,
- );
- }
- };
- }
+/** Minimal GPU flag constants for code that ORs usage/visibility bits. */
+export function installMockGPUFlags(): void {
+ globalThis.GPUBufferUsage = {
+ UNIFORM: 1,
+ VERTEX: 2,
+ COPY_DST: 4,
+ } as any;
+ globalThis.GPUShaderStage = {
+ VERTEX: 1,
+ FRAGMENT: 2,
+ } as any;
+ globalThis.GPUTextureUsage = {
+ TEXTURE_BINDING: 1,
+ COPY_DST: 2,
+ RENDER_ATTACHMENT: 4,
+ COPY_SRC: 8,
+ } as any;
}
diff --git a/packages/webgpu-renderers/src/renderer3d/glass-pipeline.ts b/packages/webgpu-renderers/src/renderer3d/glass-pipeline.ts
index f8b7fdb3..031f3737 100644
--- a/packages/webgpu-renderers/src/renderer3d/glass-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer3d/glass-pipeline.ts
@@ -7,6 +7,10 @@
import { BufferPool } from "../renderer2d/buffer-pool.js";
import { pbrGlassWgsl } from "../shaders/pbr-glass.js";
+import {
+ PREMULTIPLIED_OVER,
+ writeModelTransform,
+} from "./pipeline-variants.js";
export interface GlassUniformData {
ior?: number;
@@ -146,18 +150,7 @@ export class GlassPipeline {
targets: [
{
format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
+ blend: PREMULTIPLIED_OVER,
},
],
},
@@ -174,10 +167,9 @@ export class GlassPipeline {
// Unit quad centered at origin [-0.5, -0.5] to [0.5, 0.5]
const vertices = new Float32Array([
- -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0,
- 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0,
- 0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, -1.0,
- -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0,
+ -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0,
+ 0.0, -1.0, 0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, -1.0, -0.5, 0.5, 0.0, 0.0,
+ 1.0, 0.0, 0.0, -1.0,
]);
const indices = new Uint16Array([0, 1, 2, 0, 2, 3]);
@@ -211,31 +203,19 @@ export class GlassPipeline {
data: GlassUniformData,
): void {
// Pack model uniforms
- const mat = data.modelMatrix;
- for (let i = 0; i < 16; i++) {
- this.modelData[i] = mat[i] ?? 0;
- }
-
- const norm = data.normalMatrix ?? mat;
- for (let i = 0; i < 16; i++) {
- this.modelData[16 + i] = norm[i] ?? 0;
- }
-
- const tint = data.colorTint ?? [1, 1, 1, 1];
- this.modelData[32] = tint[0];
- this.modelData[33] = tint[1];
- this.modelData[34] = tint[2];
- this.modelData[35] = tint[3];
+ writeModelTransform(
+ this.modelData,
+ data.modelMatrix,
+ data.normalMatrix,
+ data.colorTint,
+ );
this.modelData[36] = data.opacity ?? 1.0;
this.modelData[37] = 0;
this.modelData[38] = 1.0; // twoSided
this.modelData[39] = 0;
- const modelBuffer = this.modelPool.getBuffer(
- this.device,
- this.modelData,
- );
+ const modelBuffer = this.modelPool.getBuffer(this.device, this.modelData);
// Pack glass uniforms
this.glassData[0] = data.ior ?? 1.49;
@@ -259,10 +239,7 @@ export class GlassPipeline {
this.glassData[14] = glassTint[2];
this.glassData[15] = glassTint[3];
- const glassBuffer = this.glassPool.getBuffer(
- this.device,
- this.glassData,
- );
+ const glassBuffer = this.glassPool.getBuffer(this.device, this.glassData);
const modelBindGroup = this.device.createBindGroup({
layout: this.modelLayout,
diff --git a/packages/webgpu-renderers/src/renderer3d/mesh3d-pipeline.ts b/packages/webgpu-renderers/src/renderer3d/mesh3d-pipeline.ts
index 8165447f..f2be4594 100644
--- a/packages/webgpu-renderers/src/renderer3d/mesh3d-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer3d/mesh3d-pipeline.ts
@@ -1,7 +1,12 @@
import { BufferPool } from "../renderer2d/buffer-pool.js";
import { mesh3dWgsl } from "../shaders/mesh3d.js";
import { fullCoverageView, matteLayoutEntry } from "./matte3d.js";
-import { BloomPipelineVariants } from "./pipeline-variants.js";
+import {
+ BloomPipelineVariants,
+ createBasePipelines,
+ type PipelineVariantBase,
+ writeModelTransform,
+} from "./pipeline-variants.js";
import { createLightsLayout } from "./quad3d-pipeline.js";
import { SHADOW_DEPTH_STENCIL } from "./shadow-depth.js";
import {
@@ -178,86 +183,7 @@ export class Mesh3DPipeline {
],
};
- // Single color target pipeline
- this.pipelineSingle = device.createRenderPipeline({
- label: "Mesh3DPipelineSingle",
- layout: pipelineLayout,
- vertex: vertexState,
- fragment: {
- module,
- entryPoint: "fs_single",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- depthStencil: {
- format: depthFormat,
- depthWriteEnabled: true,
- depthCompare: "less-equal",
- },
- primitive: {
- topology: "triangle-list",
- cullMode: "none",
- },
- multisample: { count: sampleCount },
- });
-
- // MRT pipeline (Color + Linear Depth)
- this.pipelineMrt = device.createRenderPipeline({
- label: "Mesh3DPipelineMrt",
- layout: pipelineLayout,
- vertex: vertexState,
- fragment: {
- module,
- entryPoint: "fs_mrt",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- {
- format: "rgba16float", // Linear depth attachment
- },
- ],
- },
- depthStencil: {
- format: depthFormat,
- depthWriteEnabled: true,
- depthCompare: "less-equal",
- },
- primitive: {
- topology: "triangle-list",
- cullMode: "none",
- },
- multisample: { count: sampleCount },
- });
-
- this.bloomVariants = new BloomPipelineVariants(device, {
+ const base: PipelineVariantBase = {
label: "Mesh3DPipeline",
layout: pipelineLayout,
vertex: vertexState,
@@ -270,7 +196,11 @@ export class Mesh3DPipeline {
},
primitive: { topology: "triangle-list", cullMode: "none" },
multisample: { count: sampleCount },
- });
+ };
+ const { single, mrt } = createBasePipelines(device, base);
+ this.pipelineSingle = single;
+ this.pipelineMrt = mrt;
+ this.bloomVariants = new BloomPipelineVariants(device, base);
this.pipelineShadow = device.createRenderPipeline({
label: "Mesh3DPipelineShadow",
@@ -334,21 +264,12 @@ export class Mesh3DPipeline {
}
private packModel(data: MeshUniformData): GPUBindGroup {
- const mat = data.modelMatrix;
- for (let i = 0; i < 16; i++) {
- this.modelData[i] = mat[i] ?? 0;
- }
-
- const norm = data.normalMatrix ?? mat;
- for (let i = 0; i < 16; i++) {
- this.modelData[16 + i] = norm[i] ?? 0;
- }
-
- const tint = data.colorTint ?? [1, 1, 1, 1];
- this.modelData[32] = tint[0];
- this.modelData[33] = tint[1];
- this.modelData[34] = tint[2];
- this.modelData[35] = tint[3];
+ writeModelTransform(
+ this.modelData,
+ data.modelMatrix,
+ data.normalMatrix,
+ data.colorTint,
+ );
this.modelData[36] = data.opacity ?? 1.0;
this.modelData[37] = data.twoSided ? 1.0 : 0.0;
diff --git a/packages/webgpu-renderers/src/renderer3d/pipeline-variants.ts b/packages/webgpu-renderers/src/renderer3d/pipeline-variants.ts
index b35bb281..d02523ed 100644
--- a/packages/webgpu-renderers/src/renderer3d/pipeline-variants.ts
+++ b/packages/webgpu-renderers/src/renderer3d/pipeline-variants.ts
@@ -4,19 +4,9 @@
* bloom variants are built on first use from the same descriptor parts.
*/
-/** Premultiplied-alpha "over". */
-export const PREMULTIPLIED_OVER: GPUBlendState = {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
-};
+import { PREMULTIPLIED_OVER } from "../renderer2d/pipeline-utils.js";
+
+export { PREMULTIPLIED_OVER };
/** Linear-depth MRT target (DoF) and HDR radiance target (bloom). */
export const LINEAR_DEPTH_FORMAT: GPUTextureFormat = "rgba16float";
@@ -33,6 +23,31 @@ export interface PipelineVariantBase {
multisample: GPUMultisampleState;
}
+/** The color pipeline and the color + linear depth (DoF) pipeline of `base`. */
+export function createBasePipelines(
+ device: GPUDevice,
+ b: PipelineVariantBase,
+): { single: GPURenderPipeline; mrt: GPURenderPipeline } {
+ const create = (useMrt: boolean) =>
+ device.createRenderPipeline({
+ label: `${b.label}${useMrt ? "Mrt" : "Single"}`,
+ layout: b.layout,
+ vertex: b.vertex,
+ fragment: {
+ module: b.module,
+ entryPoint: useMrt ? "fs_mrt" : "fs_single",
+ targets: [
+ { format: b.format, blend: PREMULTIPLIED_OVER },
+ ...(useMrt ? [{ format: LINEAR_DEPTH_FORMAT }] : []),
+ ],
+ },
+ depthStencil: b.depthStencil,
+ primitive: b.primitive,
+ multisample: b.multisample,
+ });
+ return { single: create(false), mrt: create(true) };
+}
+
export class BloomPipelineVariants {
private cache = new Map<boolean, GPURenderPipeline>();
@@ -68,3 +83,25 @@ export class BloomPipelineVariants {
return pipe;
}
}
+
+/**
+ * Writes the shared head of a model uniform block: model matrix [0..15],
+ * normal matrix [16..31] (the model matrix when absent), color tint [32..35].
+ */
+export function writeModelTransform(
+ out: Float32Array,
+ modelMatrix: ArrayLike<number>,
+ normalMatrix: ArrayLike<number> | undefined,
+ colorTint?: ArrayLike<number> | null,
+): void {
+ const norm = normalMatrix ?? modelMatrix;
+ const tint = colorTint ?? [1, 1, 1, 1];
+ for (let i = 0; i < 16; i++) {
+ out[i] = modelMatrix[i] ?? 0;
+ out[16 + i] = norm[i] ?? 0;
+ }
+ out[32] = tint[0];
+ out[33] = tint[1];
+ out[34] = tint[2];
+ out[35] = tint[3];
+}
diff --git a/packages/webgpu-renderers/src/renderer3d/quad3d-pipeline.ts b/packages/webgpu-renderers/src/renderer3d/quad3d-pipeline.ts
index e176986e..47506fd0 100644
--- a/packages/webgpu-renderers/src/renderer3d/quad3d-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer3d/quad3d-pipeline.ts
@@ -1,7 +1,12 @@
import { BufferPool } from "../renderer2d/buffer-pool.js";
import { quad3dWgsl } from "../shaders/quad3d.js";
import { fullCoverageView, matteLayoutEntry } from "./matte3d.js";
-import { BloomPipelineVariants } from "./pipeline-variants.js";
+import {
+ BloomPipelineVariants,
+ createBasePipelines,
+ type PipelineVariantBase,
+ writeModelTransform,
+} from "./pipeline-variants.js";
import { SHADOW_DEPTH_STENCIL } from "./shadow-depth.js";
import {
packSurfaceParams,
@@ -191,87 +196,7 @@ export class Quad3DPipeline {
],
};
- // 1. Single color target pipeline
- this.pipelineSingle = device.createRenderPipeline({
- label: "Quad3DPipelineSingle",
- layout: pipelineLayout,
- vertex: vertexState,
- fragment: {
- module,
- entryPoint: "fs_single",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- ],
- },
- depthStencil: {
- format: depthFormat,
- depthWriteEnabled: true,
- depthCompare: "less-equal",
- },
- primitive: {
- topology: "triangle-list",
- cullMode: "none",
- },
- multisample: { count: sampleCount },
- });
-
- // 2. MRT pipeline (Color + Linear Depth)
- this.pipelineMrt = device.createRenderPipeline({
- label: "Quad3DPipelineMrt",
- layout: pipelineLayout,
- vertex: vertexState,
- fragment: {
- module,
- entryPoint: "fs_mrt",
- targets: [
- {
- format,
- blend: {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- },
- },
- {
- format: "rgba16float", // Linear depth attachment
- },
- ],
- },
- depthStencil: {
- format: depthFormat,
- depthWriteEnabled: true,
- depthCompare: "less-equal",
- },
- primitive: {
- topology: "triangle-list",
- cullMode: "none",
- },
- multisample: { count: sampleCount },
- });
-
- // Unit quad centered at origin: [-0.5, -0.5] to [0.5, 0.5] with normal [0, 0, -1]
- this.bloomVariants = new BloomPipelineVariants(device, {
+ const base: PipelineVariantBase = {
label: "Quad3DPipeline",
layout: pipelineLayout,
vertex: vertexState,
@@ -284,7 +209,11 @@ export class Quad3DPipeline {
},
primitive: { topology: "triangle-list", cullMode: "none" },
multisample: { count: sampleCount },
- });
+ };
+ const { single, mrt } = createBasePipelines(device, base);
+ this.pipelineSingle = single;
+ this.pipelineMrt = mrt;
+ this.bloomVariants = new BloomPipelineVariants(device, base);
this.pipelineShadow = device.createRenderPipeline({
label: "Quad3DPipelineShadow",
@@ -330,21 +259,12 @@ export class Quad3DPipeline {
}
private packModel(data: ModelUniformData): GPUBindGroup {
- const mat = data.modelMatrix;
- for (let i = 0; i < 16; i++) {
- this.modelData[i] = mat[i] ?? 0;
- }
-
- const norm = data.normalMatrix ?? mat;
- for (let i = 0; i < 16; i++) {
- this.modelData[16 + i] = norm[i] ?? 0;
- }
-
- const tint = data.colorTint ?? [1, 1, 1, 1];
- this.modelData[32] = tint[0];
- this.modelData[33] = tint[1];
- this.modelData[34] = tint[2];
- this.modelData[35] = tint[3];
+ writeModelTransform(
+ this.modelData,
+ data.modelMatrix,
+ data.normalMatrix,
+ data.colorTint,
+ );
const bColor = data.borderColor ?? [0, 0, 0, 0];
this.modelData[36] = bColor[0];
diff --git a/packages/webgpu-renderers/src/renderer3d/slug3d-pipeline.ts b/packages/webgpu-renderers/src/renderer3d/slug3d-pipeline.ts
index 94b0bfa5..23a136d1 100644
--- a/packages/webgpu-renderers/src/renderer3d/slug3d-pipeline.ts
+++ b/packages/webgpu-renderers/src/renderer3d/slug3d-pipeline.ts
@@ -3,7 +3,11 @@ import { BufferPool } from "../renderer2d/buffer-pool.js";
import { slug3dWgsl } from "../shaders/slug3d.js";
import type { SlugFont } from "../slug/slug-loader.js";
import { fullCoverageView, matteLayoutEntry } from "./matte3d.js";
-import { BloomPipelineVariants } from "./pipeline-variants.js";
+import {
+ BloomPipelineVariants,
+ createBasePipelines,
+ type PipelineVariantBase,
+} from "./pipeline-variants.js";
import {
type MaterialUniformData,
packMaterialParams,
@@ -120,18 +124,6 @@ export class Slug3DPipeline {
],
};
- const blend: GPUBlendState = {
- color: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- alpha: {
- srcFactor: "one",
- dstFactor: "one-minus-src-alpha",
- operation: "add",
- },
- };
const depthStencil: GPUDepthStencilState = {
format: depthFormat,
depthWriteEnabled: true,
@@ -143,35 +135,7 @@ export class Slug3DPipeline {
};
const multisample: GPUMultisampleState = { count: sampleCount };
- this.pipelineSingle = device.createRenderPipeline({
- label: "Slug3DPipelineSingle",
- layout,
- vertex,
- fragment: {
- module,
- entryPoint: "fs_single",
- targets: [{ format, blend }],
- },
- depthStencil,
- primitive,
- multisample,
- });
-
- this.pipelineMrt = device.createRenderPipeline({
- label: "Slug3DPipelineMrt",
- layout,
- vertex,
- fragment: {
- module,
- entryPoint: "fs_mrt",
- targets: [{ format, blend }, { format: "rgba16float" }],
- },
- depthStencil,
- primitive,
- multisample,
- });
-
- this.bloomVariants = new BloomPipelineVariants(device, {
+ const base: PipelineVariantBase = {
label: "Slug3DPipeline",
layout,
vertex,
@@ -180,7 +144,11 @@ export class Slug3DPipeline {
depthStencil,
primitive,
multisample,
- });
+ };
+ const { single, mrt } = createBasePipelines(device, base);
+ this.pipelineSingle = single;
+ this.pipelineMrt = mrt;
+ this.bloomVariants = new BloomPipelineVariants(device, base);
}
resetPools(): void {
diff --git a/packages/webgpu-renderers/src/signals/audio-bass-match.test.ts b/packages/webgpu-renderers/src/signals/audio-bass-match.test.ts
index aab6afe0..a7b07a03 100644
--- a/packages/webgpu-renderers/src/signals/audio-bass-match.test.ts
+++ b/packages/webgpu-renderers/src/signals/audio-bass-match.test.ts
@@ -3,33 +3,7 @@ import path from "node:path";
import { describe, expect, it } from "vitest";
import { ensureDevice } from "../device.js";
import { AudioSignalComputePipeline } from "./audio-signal-compute.js";
-
-function loadWavChannels(filePath: string): {
- channels: Float32Array[];
- sampleRate: number;
-} {
- const buf = fs.readFileSync(filePath);
- const numChannels = buf.readUInt16LE(22);
- const sampleRate = buf.readUInt32LE(24);
- let offset = 12;
- while (offset < buf.length - 8) {
- const id = buf.toString("ascii", offset, offset + 4);
- const size = buf.readUInt32LE(offset + 4);
- if (id === "data") {
- offset += 8;
- break;
- }
- offset += 8 + size;
- }
- const numSamples = Math.floor((buf.length - offset) / (numChannels * 2));
- const left = new Float32Array(numSamples);
- const right = new Float32Array(numSamples);
- for (let i = 0; i < numSamples; i++) {
- left[i] = buf.readInt16LE(offset + i * 4) / 32768.0;
- right[i] = buf.readInt16LE(offset + i * 4 + 2) / 32768.0;
- }
- return { channels: [left, right], sampleRate };
-}
+import { loadWavChannels } from "./test-helpers.js";
describe("WebGPU vs Python Bass Signal Matching", () => {
it("matches Python bass signal extraction with WebGPU compute shaders", async () => {
diff --git a/packages/webgpu-renderers/src/signals/audio-signal-compute.test.ts b/packages/webgpu-renderers/src/signals/audio-signal-compute.test.ts
index c842bf14..2929fb02 100644
--- a/packages/webgpu-renderers/src/signals/audio-signal-compute.test.ts
+++ b/packages/webgpu-renderers/src/signals/audio-signal-compute.test.ts
@@ -3,33 +3,7 @@ import path from "node:path";
import { describe, expect, it } from "vitest";
import { ensureDevice } from "../device.js";
import { AudioSignalComputePipeline } from "./audio-signal-compute.js";
-
-function loadWavChannels(filePath: string): {
- channels: Float32Array[];
- sampleRate: number;
-} {
- const buf = fs.readFileSync(filePath);
- const numChannels = buf.readUInt16LE(22);
- const sampleRate = buf.readUInt32LE(24);
- let offset = 12;
- while (offset < buf.length - 8) {
- const id = buf.toString("ascii", offset, offset + 4);
- const size = buf.readUInt32LE(offset + 4);
- if (id === "data") {
- offset += 8;
- break;
- }
- offset += 8 + size;
- }
- const numSamples = Math.floor((buf.length - offset) / (numChannels * 2));
- const left = new Float32Array(numSamples);
- const right = new Float32Array(numSamples);
- for (let i = 0; i < numSamples; i++) {
- left[i] = buf.readInt16LE(offset + i * 4) / 32768.0;
- right[i] = buf.readInt16LE(offset + i * 4 + 2) / 32768.0;
- }
- return { channels: [left, right], sampleRate };
-}
+import { loadWavChannels } from "./test-helpers.js";
describe("AudioSignalComputePipeline WebGPU Extraction", () => {
it("extracts real audio features from user WAV file on GPU", async () => {
diff --git a/packages/webgpu-renderers/src/signals/signal-math.test.ts b/packages/webgpu-renderers/src/signals/signal-math.test.ts
index 40be7572..e01cc7a7 100644
--- a/packages/webgpu-renderers/src/signals/signal-math.test.ts
+++ b/packages/webgpu-renderers/src/signals/signal-math.test.ts
@@ -1,79 +1,15 @@
-import { describe, expect, it, vi } from "vitest";
-
-// Polyfill WebGPU globals for test runner
-if (typeof globalThis.GPUBufferUsage === "undefined") {
- globalThis.GPUBufferUsage = {
- MAP_READ: 1,
- MAP_WRITE: 2,
- COPY_SRC: 4,
- COPY_DST: 8,
- INDEX: 16,
- VERTEX: 32,
- UNIFORM: 64,
- STORAGE: 128,
- INDIRECT: 256,
- QUERY_RESOLVE: 512,
- } as unknown as typeof GPUBufferUsage;
-}
-
-if (typeof globalThis.GPUTextureUsage === "undefined") {
- globalThis.GPUTextureUsage = {
- COPY_SRC: 1,
- COPY_DST: 2,
- TEXTURE_BINDING: 4,
- STORAGE_BINDING: 8,
- RENDER_ATTACHMENT: 16,
- } as unknown as typeof GPUTextureUsage;
-}
+import { describe, expect, it } from "vitest";
import { SignalRegistry } from "./signal-registry.js";
+import { createRenderMocks, installGPUUsageGlobals } from "./test-helpers.js";
+
+installGPUUsageGlobals();
describe("SignalRegistry - SignalMath WebGPU Dispatch", () => {
it("dispatches remap operation and sets computed stats", () => {
const registry = new SignalRegistry();
- const mockTexture = {
- createView: vi.fn().mockReturnValue({}),
- destroy: vi.fn(),
- width: 256,
- height: 1,
- } as unknown as GPUTexture;
-
- const mockBuffer = {
- destroy: vi.fn(),
- } as unknown as GPUBuffer;
-
- const mockPipeline = {
- getBindGroupLayout: vi.fn().mockReturnValue({}),
- } as unknown as GPURenderPipeline;
-
- const mockPass = {
- setPipeline: vi.fn(),
- setBindGroup: vi.fn(),
- draw: vi.fn(),
- end: vi.fn(),
- };
-
- const mockEncoder = {
- beginRenderPass: vi.fn().mockReturnValue(mockPass),
- copyBufferToTexture: vi.fn(),
- finish: vi.fn().mockReturnValue({}),
- } as unknown as GPUCommandEncoder;
-
- const mockDevice = {
- createTexture: vi.fn().mockReturnValue(mockTexture),
- createBuffer: vi.fn().mockReturnValue(mockBuffer),
- createSampler: vi.fn().mockReturnValue({}),
- createShaderModule: vi.fn().mockReturnValue({}),
- createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
- createBindGroup: vi.fn().mockReturnValue({}),
- createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
- queue: {
- writeBuffer: vi.fn(),
- writeTexture: vi.fn(),
- submit: vi.fn(),
- },
- } as unknown as GPUDevice;
+ const { mockEncoder, mockDevice } = createRenderMocks(256, 1);
const nodeId = "math-remap-1";
const signalData = {
@@ -114,48 +50,7 @@ describe("SignalRegistry - SignalMath WebGPU Dispatch", () => {
it("compiles and dispatches custom WGSL mode", () => {
const registry = new SignalRegistry();
- const mockTexture = {
- createView: vi.fn().mockReturnValue({}),
- destroy: vi.fn(),
- width: 128,
- height: 1,
- } as unknown as GPUTexture;
-
- const mockBuffer = {
- destroy: vi.fn(),
- } as unknown as GPUBuffer;
-
- const mockPipeline = {
- getBindGroupLayout: vi.fn().mockReturnValue({}),
- } as unknown as GPURenderPipeline;
-
- const mockPass = {
- setPipeline: vi.fn(),
- setBindGroup: vi.fn(),
- draw: vi.fn(),
- end: vi.fn(),
- };
-
- const mockEncoder = {
- beginRenderPass: vi.fn().mockReturnValue(mockPass),
- copyBufferToTexture: vi.fn(),
- finish: vi.fn().mockReturnValue({}),
- } as unknown as GPUCommandEncoder;
-
- const mockDevice = {
- createTexture: vi.fn().mockReturnValue(mockTexture),
- createBuffer: vi.fn().mockReturnValue(mockBuffer),
- createSampler: vi.fn().mockReturnValue({}),
- createShaderModule: vi.fn().mockReturnValue({}),
- createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
- createBindGroup: vi.fn().mockReturnValue({}),
- createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
- queue: {
- writeBuffer: vi.fn(),
- writeTexture: vi.fn(),
- submit: vi.fn(),
- },
- } as unknown as GPUDevice;
+ const { mockEncoder, mockDevice } = createRenderMocks(128, 1);
const nodeId = "math-custom-1";
const customWGSL = "return pow(a, 2.5) * b;";
@@ -189,48 +84,7 @@ describe("SignalRegistry - SignalMath WebGPU Dispatch", () => {
it("dispatches add operation with constant value and computes accurate shifted stats", () => {
const registry = new SignalRegistry();
- const mockTexture = {
- createView: vi.fn().mockReturnValue({}),
- destroy: vi.fn(),
- width: 256,
- height: 1,
- } as unknown as GPUTexture;
-
- const mockBuffer = {
- destroy: vi.fn(),
- } as unknown as GPUBuffer;
-
- const mockPipeline = {
- getBindGroupLayout: vi.fn().mockReturnValue({}),
- } as unknown as GPURenderPipeline;
-
- const mockPass = {
- setPipeline: vi.fn(),
- setBindGroup: vi.fn(),
- draw: vi.fn(),
- end: vi.fn(),
- };
-
- const mockEncoder = {
- beginRenderPass: vi.fn().mockReturnValue(mockPass),
- copyBufferToTexture: vi.fn(),
- finish: vi.fn().mockReturnValue({}),
- } as unknown as GPUCommandEncoder;
-
- const mockDevice = {
- createTexture: vi.fn().mockReturnValue(mockTexture),
- createBuffer: vi.fn().mockReturnValue(mockBuffer),
- createSampler: vi.fn().mockReturnValue({}),
- createShaderModule: vi.fn().mockReturnValue({}),
- createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
- createBindGroup: vi.fn().mockReturnValue({}),
- createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
- queue: {
- writeBuffer: vi.fn(),
- writeTexture: vi.fn(),
- submit: vi.fn(),
- },
- } as unknown as GPUDevice;
+ const { mockEncoder, mockDevice } = createRenderMocks(256, 1);
const nodeId = "math-add-1";
const signalData = {
@@ -266,48 +120,7 @@ describe("SignalRegistry - SignalMath WebGPU Dispatch", () => {
it("resolves signalB when passed as a raw number input instead of config bValue", () => {
const registry = new SignalRegistry();
- const mockTexture = {
- createView: vi.fn().mockReturnValue({}),
- destroy: vi.fn(),
- width: 256,
- height: 1,
- } as unknown as GPUTexture;
-
- const mockBuffer = {
- destroy: vi.fn(),
- } as unknown as GPUBuffer;
-
- const mockPipeline = {
- getBindGroupLayout: vi.fn().mockReturnValue({}),
- } as unknown as GPURenderPipeline;
-
- const mockPass = {
- setPipeline: vi.fn(),
- setBindGroup: vi.fn(),
- draw: vi.fn(),
- end: vi.fn(),
- };
-
- const mockEncoder = {
- beginRenderPass: vi.fn().mockReturnValue(mockPass),
- copyBufferToTexture: vi.fn(),
- finish: vi.fn().mockReturnValue({}),
- } as unknown as GPUCommandEncoder;
-
- const mockDevice = {
- createTexture: vi.fn().mockReturnValue(mockTexture),
- createBuffer: vi.fn().mockReturnValue(mockBuffer),
- createSampler: vi.fn().mockReturnValue({}),
- createShaderModule: vi.fn().mockReturnValue({}),
- createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
- createBindGroup: vi.fn().mockReturnValue({}),
- createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
- queue: {
- writeBuffer: vi.fn(),
- writeTexture: vi.fn(),
- submit: vi.fn(),
- },
- } as unknown as GPUDevice;
+ const { mockEncoder, mockDevice } = createRenderMocks(256, 1);
const nodeId = "math-add-raw-num";
const signalData = {
@@ -343,48 +156,7 @@ describe("SignalRegistry - SignalMath WebGPU Dispatch", () => {
it("invalidates downstream signal math cache and updates stats when upstream number or input changes", () => {
const registry = new SignalRegistry();
- const mockTexture = {
- createView: vi.fn().mockReturnValue({}),
- destroy: vi.fn(),
- width: 256,
- height: 1,
- } as unknown as GPUTexture;
-
- const mockBuffer = {
- destroy: vi.fn(),
- } as unknown as GPUBuffer;
-
- const mockPipeline = {
- getBindGroupLayout: vi.fn().mockReturnValue({}),
- } as unknown as GPURenderPipeline;
-
- const mockPass = {
- setPipeline: vi.fn(),
- setBindGroup: vi.fn(),
- draw: vi.fn(),
- end: vi.fn(),
- };
-
- const mockEncoder = {
- beginRenderPass: vi.fn().mockReturnValue(mockPass),
- copyBufferToTexture: vi.fn(),
- finish: vi.fn().mockReturnValue({}),
- } as unknown as GPUCommandEncoder;
-
- const mockDevice = {
- createTexture: vi.fn().mockReturnValue(mockTexture),
- createBuffer: vi.fn().mockReturnValue(mockBuffer),
- createSampler: vi.fn().mockReturnValue({}),
- createShaderModule: vi.fn().mockReturnValue({}),
- createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
- createBindGroup: vi.fn().mockReturnValue({}),
- createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
- queue: {
- writeBuffer: vi.fn(),
- writeTexture: vi.fn(),
- submit: vi.fn(),
- },
- } as unknown as GPURenderPipeline as unknown as GPUDevice;
+ const { mockEncoder, mockDevice } = createRenderMocks(256, 1);
// Set initial upstream base signal stats
registry.setStats("source-audio", { min: 0.1, max: 0.9 });
@@ -487,48 +259,7 @@ describe("SignalRegistry - SignalMath WebGPU Dispatch", () => {
it("recalculates stats immediately when operation changes from Add to Subtract without changing bValue", () => {
const registry = new SignalRegistry();
- const mockTexture = {
- createView: vi.fn().mockReturnValue({}),
- destroy: vi.fn(),
- width: 256,
- height: 1,
- } as unknown as GPUTexture;
-
- const mockBuffer = {
- destroy: vi.fn(),
- } as unknown as GPUBuffer;
-
- const mockPipeline = {
- getBindGroupLayout: vi.fn().mockReturnValue({}),
- } as unknown as GPURenderPipeline;
-
- const mockPass = {
- setPipeline: vi.fn(),
- setBindGroup: vi.fn(),
- draw: vi.fn(),
- end: vi.fn(),
- };
-
- const mockEncoder = {
- beginRenderPass: vi.fn().mockReturnValue(mockPass),
- copyBufferToTexture: vi.fn(),
- finish: vi.fn().mockReturnValue({}),
- } as unknown as GPUCommandEncoder;
-
- const mockDevice = {
- createTexture: vi.fn().mockReturnValue(mockTexture),
- createBuffer: vi.fn().mockReturnValue(mockBuffer),
- createSampler: vi.fn().mockReturnValue({}),
- createShaderModule: vi.fn().mockReturnValue({}),
- createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
- createBindGroup: vi.fn().mockReturnValue({}),
- createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
- queue: {
- writeBuffer: vi.fn(),
- writeTexture: vi.fn(),
- submit: vi.fn(),
- },
- } as unknown as GPURenderPipeline as unknown as GPUDevice;
+ const { mockEncoder, mockDevice } = createRenderMocks(256, 1);
registry.setStats("source-sig", { min: 2.0, max: 5.0 });
@@ -594,48 +325,7 @@ describe("SignalRegistry - SignalMath WebGPU Dispatch", () => {
it("does not clamp add operation to [0, 1] range", () => {
const registry = new SignalRegistry();
- const mockTexture = {
- createView: vi.fn().mockReturnValue({}),
- destroy: vi.fn(),
- width: 256,
- height: 1,
- } as unknown as GPUTexture;
-
- const mockBuffer = {
- destroy: vi.fn(),
- } as unknown as GPUBuffer;
-
- const mockPipeline = {
- getBindGroupLayout: vi.fn().mockReturnValue({}),
- } as unknown as GPURenderPipeline;
-
- const mockPass = {
- setPipeline: vi.fn(),
- setBindGroup: vi.fn(),
- draw: vi.fn(),
- end: vi.fn(),
- };
-
- const mockEncoder = {
- beginRenderPass: vi.fn().mockReturnValue(mockPass),
- copyBufferToTexture: vi.fn(),
- finish: vi.fn().mockReturnValue({}),
- } as unknown as GPUCommandEncoder;
-
- const mockDevice = {
- createTexture: vi.fn().mockReturnValue(mockTexture),
- createBuffer: vi.fn().mockReturnValue(mockBuffer),
- createSampler: vi.fn().mockReturnValue({}),
- createShaderModule: vi.fn().mockReturnValue({}),
- createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
- createBindGroup: vi.fn().mockReturnValue({}),
- createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
- queue: {
- writeBuffer: vi.fn(),
- writeTexture: vi.fn(),
- submit: vi.fn(),
- },
- } as unknown as GPUDevice;
+ const { mockEncoder, mockDevice } = createRenderMocks(256, 1);
registry.setStats("source-sig", { min: 0.636, max: 1.15 });
diff --git a/packages/webgpu-renderers/src/signals/signal-registry.test.ts b/packages/webgpu-renderers/src/signals/signal-registry.test.ts
index a98f344e..bb4ab367 100644
--- a/packages/webgpu-renderers/src/signals/signal-registry.test.ts
+++ b/packages/webgpu-renderers/src/signals/signal-registry.test.ts
@@ -1,32 +1,9 @@
import { describe, expect, it, vi } from "vitest";
-// Polyfill WebGPU globals for the test runner environment
-if (typeof globalThis.GPUBufferUsage === "undefined") {
- globalThis.GPUBufferUsage = {
- MAP_READ: 1,
- MAP_WRITE: 2,
- COPY_SRC: 4,
- COPY_DST: 8,
- INDEX: 16,
- VERTEX: 32,
- UNIFORM: 64,
- STORAGE: 128,
- INDIRECT: 256,
- QUERY_RESOLVE: 512,
- } as unknown as typeof GPUBufferUsage;
-}
-
-if (typeof globalThis.GPUTextureUsage === "undefined") {
- globalThis.GPUTextureUsage = {
- COPY_SRC: 1,
- COPY_DST: 2,
- TEXTURE_BINDING: 4,
- STORAGE_BINDING: 8,
- RENDER_ATTACHMENT: 16,
- } as unknown as typeof GPUTextureUsage;
-}
+installGPUUsageGlobals();
import { SignalRegistry } from "./signal-registry.js";
+import { installGPUUsageGlobals } from "./test-helpers.js";
describe("SignalRegistry", () => {
it("should allocate, cache, and reuse 1D buffers", () => {
diff --git a/packages/webgpu-renderers/src/signals/test-helpers.ts b/packages/webgpu-renderers/src/signals/test-helpers.ts
new file mode 100644
index 00000000..f666ab1f
--- /dev/null
+++ b/packages/webgpu-renderers/src/signals/test-helpers.ts
@@ -0,0 +1,123 @@
+import fs from "node:fs";
+import { type Mock, vi } from "vitest";
+
+/** GPUBufferUsage / GPUTextureUsage flags, which Node lacks without a device. */
+export function installGPUUsageGlobals(): void {
+ if (typeof globalThis.GPUBufferUsage === "undefined") {
+ globalThis.GPUBufferUsage = {
+ MAP_READ: 1,
+ MAP_WRITE: 2,
+ COPY_SRC: 4,
+ COPY_DST: 8,
+ INDEX: 16,
+ VERTEX: 32,
+ UNIFORM: 64,
+ STORAGE: 128,
+ INDIRECT: 256,
+ QUERY_RESOLVE: 512,
+ } as unknown as typeof GPUBufferUsage;
+ }
+
+ if (typeof globalThis.GPUTextureUsage === "undefined") {
+ globalThis.GPUTextureUsage = {
+ COPY_SRC: 1,
+ COPY_DST: 2,
+ TEXTURE_BINDING: 4,
+ STORAGE_BINDING: 8,
+ RENDER_ATTACHMENT: 16,
+ } as unknown as typeof GPUTextureUsage;
+ }
+}
+
+/** A mock device whose render pipeline draws into a `width`×`height` texture. */
+export function createRenderMocks(
+ width: number,
+ height: number,
+): {
+ mockTexture: GPUTexture;
+ mockBuffer: GPUBuffer;
+ mockPipeline: GPURenderPipeline;
+ mockPass: Record<"setPipeline" | "setBindGroup" | "draw" | "end", Mock>;
+ mockEncoder: GPUCommandEncoder;
+ mockDevice: GPUDevice;
+} {
+ const mockTexture = {
+ createView: vi.fn().mockReturnValue({}),
+ destroy: vi.fn(),
+ width,
+ height,
+ } as unknown as GPUTexture;
+
+ const mockBuffer = {
+ destroy: vi.fn(),
+ } as unknown as GPUBuffer;
+
+ const mockPipeline = {
+ getBindGroupLayout: vi.fn().mockReturnValue({}),
+ } as unknown as GPURenderPipeline;
+
+ const mockPass = {
+ setPipeline: vi.fn(),
+ setBindGroup: vi.fn(),
+ draw: vi.fn(),
+ end: vi.fn(),
+ };
+
+ const mockEncoder = {
+ beginRenderPass: vi.fn().mockReturnValue(mockPass),
+ copyBufferToTexture: vi.fn(),
+ finish: vi.fn().mockReturnValue({}),
+ } as unknown as GPUCommandEncoder;
+
+ const mockDevice = {
+ createTexture: vi.fn().mockReturnValue(mockTexture),
+ createBuffer: vi.fn().mockReturnValue(mockBuffer),
+ createSampler: vi.fn().mockReturnValue({}),
+ createShaderModule: vi.fn().mockReturnValue({}),
+ createRenderPipeline: vi.fn().mockReturnValue(mockPipeline),
+ createBindGroup: vi.fn().mockReturnValue({}),
+ createCommandEncoder: vi.fn().mockReturnValue(mockEncoder),
+ queue: {
+ writeBuffer: vi.fn(),
+ writeTexture: vi.fn(),
+ submit: vi.fn(),
+ },
+ } as unknown as GPUDevice;
+
+ return {
+ mockTexture,
+ mockBuffer,
+ mockPipeline,
+ mockPass,
+ mockEncoder,
+ mockDevice,
+ };
+}
+
+/** The channels of a 16-bit stereo PCM WAV file. */
+export function loadWavChannels(filePath: string): {
+ channels: Float32Array[];
+ sampleRate: number;
+} {
+ const buf = fs.readFileSync(filePath);
+ const numChannels = buf.readUInt16LE(22);
+ const sampleRate = buf.readUInt32LE(24);
+ let offset = 12;
+ while (offset < buf.length - 8) {
+ const id = buf.toString("ascii", offset, offset + 4);
+ const size = buf.readUInt32LE(offset + 4);
+ if (id === "data") {
+ offset += 8;
+ break;
+ }
+ offset += 8 + size;
+ }
+ const numSamples = Math.floor((buf.length - offset) / (numChannels * 2));
+ const left = new Float32Array(numSamples);
+ const right = new Float32Array(numSamples);
+ for (let i = 0; i < numSamples; i++) {
+ left[i] = buf.readInt16LE(offset + i * 4) / 32768.0;
+ right[i] = buf.readInt16LE(offset + i * 4 + 2) / 32768.0;
+ }
+ return { channels: [left, right], sampleRate };
+}
diff --git a/packages/webgpu-renderers/tsdown.config.ts b/packages/webgpu-renderers/tsdown.config.ts
new file mode 100644
index 00000000..eef1364b
--- /dev/null
+++ b/packages/webgpu-renderers/tsdown.config.ts
@@ -0,0 +1,13 @@
+import { defineConfig } from "tsdown";
+
+export default defineConfig({
+ entry: [
+ "src/index.ts",
+ "src/math3d/index.ts",
+ "src/text-animations/index.ts",
+ ],
+ format: ["esm"],
+ dts: true,
+ clean: true,
+ sourcemap: true,
+});