Skip to content

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

2.9 KB
Raw Blame
import { ensureDevice } from "@framefields/webgpu-renderers";
import { describe, expect, it } from "vitest";
import { TensorPipeline } from "../pipeline/tensor-pipeline.js";
import { FaceLandmarksComputePipeline } from "../pipelines/face-landmarks-pipeline.js";
import type { NormalizedLandmarkList } from "../types.js";

describe("Native WebGPU SDXL ControlNet Face Landmarks Compute Pipeline", () => {
	it("rasterizes facial mesh contours directly to GPUTexture in VRAM under 1ms", async () => {
		let device: GPUDevice;
		try {
			device = await ensureDevice();
		} catch (e) {
			console.warn(
				"WebGPU device unavailable in test environment, skipping:",
				e,
			);
			return;
		}

		const width = 256;
		const height = 256;

		// 478 standard MediaPipe face landmarks in normalized [0, 1] coordinates
		const mockLandmarks: NormalizedLandmarkList = Array.from(
			{ length: 478 },
			(_, i) => ({
				x: 0.5 + Math.sin(i * 0.1) * 0.25,
				y: 0.5 + Math.cos(i * 0.1) * 0.25,
				z: -0.05,
				visibility: 0.99,
			}),
		);

		const pipeline = new FaceLandmarksComputePipeline(device);

		// Warmup pass
		pipeline.execute(mockLandmarks, { width, height, lineWidth: 4 });
		await device.queue.onSubmittedWorkDone();

		const t0 = performance.now();
		const texture = pipeline.execute(mockLandmarks, {
			width,
			height,
			lineWidth: 4,
		});
		await device.queue.onSubmittedWorkDone();
		const elapsed = performance.now() - t0;

		expect(texture).toBeDefined();
		expect(texture.width).toBe(width);
		expect(texture.height).toBe(height);
		expect(texture.format).toBe("rgba8unorm");

		console.log(
			`[Native WebGPU FaceLandmarks] Render time: ${elapsed.toFixed(2)} ms`,
		);
		expect(elapsed).toBeLessThan(30.0); // generous ceiling for headless CI and parallel test runs

		pipeline.destroy();
	});

	it("integrates seamlessly into fluent TensorPipeline and ControlNetMultiplexer for SDXL", async () => {
		let device: GPUDevice;
		try {
			device = await ensureDevice();
		} catch (e) {
			console.warn(
				"WebGPU device unavailable in test environment, skipping:",
				e,
			);
			return;
		}

		const width = 128;
		const height = 128;

		const sourceTex = device.createTexture({
			size: { width, height },
			format: "rgba8unorm",
			usage:
				GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT,
		});

		const mockLandmarks: NormalizedLandmarkList = Array.from(
			{ length: 478 },
			(_, i) => ({
				x: 0.5 + Math.sin(i) * 0.2,
				y: 0.5 + Math.cos(i) * 0.2,
				z: 0.0,
				visibility: 0.95,
			}),
		);

		const pipeline = TensorPipeline.from(sourceTex).face(mockLandmarks, {
			width,
			height,
			lineWidth: 3,
		});

		const outputs = await pipeline.execute(device);
		expect(outputs.faceLandmarks).toBeDefined();
		expect(outputs.faceLandmarks!.width).toBe(width);
		expect(outputs.faceLandmarks!.height).toBe(height);

		pipeline.destroy();
		sourceTex.destroy();
	});
});