Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
docs(webcam): record where segmentation stands and what has to be measured
EtienneLescot4ad25acbb2c8
diff --git a/technical-documentation/engineering/webcam-segmentation-bench-brief.md b/technical-documentation/engineering/webcam-segmentation-bench-brief.mdnew file mode 100644index 000000000..1f3ee0b22--- /dev/null+++ b/technical-documentation/engineering/webcam-segmentation-bench-brief.md@@ -0,0 +1,147 @@+# Brief — webcam segmentation feasibility measurement++Self-contained brief for an agent running on a test machine. Everything needed is here; read+[webcam-segmentation.md](webcam-segmentation.md) for the surrounding decision.++---++## Mission++Answer one question with admissible numbers:++> **Can the target GPU run a 256×144 segmentation model once per frame while the compositor+> still holds its frame budget?**++A "yes" unlocks moving AI segmentation into the native compositor. A "no" kills that design and+we keep segmenting in the renderer. **You are not implementing the feature. You are measuring+whether it is affordable.**++## Hard constraints++- **Do not modify any production code.** No changes under `src/`, `electron/`, or+ `crates/compositor/`. Measurement scaffolding lives in a scratch directory or a throwaway+ crate you do not commit.+- **Do not add dependencies to the app.** Nothing enters `package.json`, `Cargo.toml`, or the+ packaging config. Install what you need on the machine, outside the repo.+- **Do not push, do not open a PR.** Report back; a human decides.+- **Do not download model weights without naming the source and getting it approved.** Prefer+ converting the model already vendored in the repo — see Task 1.++## Environment++Repo: `getopenscreen/openscreen`, branch `feat/webcam-effects`.++Build wrapper: `crates\x.bat` (`x.bat run --release -- …`) — it sets up vcvars, puts ffmpeg on+PATH, and calls cargo. Two things break on a fresh machine:++- its hardcoded Visual Studio path (`…\Visual Studio\18\Insiders\…`) may not exist — the file's+ own comment says to adjust it;+- if you are in a **git worktree**, `crates/thirdparty/` is absent (gitignored). Copy it from a+ full checkout or nothing builds. Bindgen also needs `LIBCLANG_PATH` pointing at an LLVM `bin`.++The bench needs a fixture directory containing `screen.mp4`, `webcam.mp4` and+`screen.cursor.json`. `crates/fixture/fixture.json` is the frozen manifest; regenerate with+`-c copy` so the bitstream is untouched.++**Report the hardware.** GPU model, driver version, whether it is integrated or discrete, and+whether the machine is on battery. This matters more than any number you produce: the design+targets an **integrated** GPU. If this machine has a discrete GPU, say so prominently — the+result does not transfer, and you should state that rather than let it read as a green light.++## Measurement discipline — non-negotiable++Governed by `technical-documentation/engineering/rendering-performance.md` §C.2. That document+records several runs that were **VOID** and explains why. Follow it or your numbers are worthless:++- discard one full warm-up sweep before anything you report;+- `--repeat 3`, and interleave A/B/A/B — never all-A-then-all-B;+- **spread gate: any arm above 15 % spread declares the run VOID.** Report it as VOID; do not+ quote it;+- quiet machine. The documented VOID example had ~40 browser and Electron processes live. Close+ them;+- thermal and battery drift have *inverted conclusions* on this hardware before. Re-run if in doubt;+- **only within-run ratios transfer between machines, never absolute times.** Your deliverable is+ a ratio.++---++## Task 1 — isolated inference latency++**Goal:** ms per inference, warm, p50 and p95, batch 1.++**Model.** MediaPipe SelfieSegmentation, *landscape* variant. The repo already vendors the+weights at `public/mediapipe/selfie_segmentation/selfie_segmentation_landscape.tflite`+(249,792 bytes). This is the runtime configuration in use today+(`webcamSegmentation.ts`: `modelSelection: 1`).++Preferred route — **convert what we already ship**, so no new supply chain:++1. Install `tensorflow` + `tf2onnx` on the test machine (this is fine here; it is not fine on a+ dev machine and must never enter the repo's dependencies).+2. Convert the vendored `.tflite` to ONNX.+3. **Derive the input/output tensor shapes and dtypes from the converted model — do not assume+ them.** Report what you find.+4. Sanity-check correctness on one real webcam frame: run it and confirm the mask actually+ segments a person. A latency number for a broken graph is worthless.++If conversion proves impractical, stop and report that, naming what failed. Do not substitute a+pre-converted model from the internet without the source being approved first.++**Measure.** ONNX Runtime with the **DirectML** execution provider. Report:++- p50 / p95 ms per inference, warm, over ≥ 200 iterations after ≥ 20 discarded;+- the same on the **CPU** execution provider, as a floor to compare against;+- ONNX Runtime version, DirectML version, and the ops that fell back to CPU if any.++## Task 2 — contention++**Goal:** does inference running alongside compositing cost the compositor its budget?++This needs **no integration work**. Two arms:++- **Arm A:** the C8 bench alone.+ ```+ crates\x.bat run --release -- --cfg C8 --fixture fixture --repeat 3 --out out\+ ```+- **Arm B:** the same, while a separate process runs the Task 1 inference in a loop at **60 Hz**+ (one inference every 16.7 ms) on the same GPU.++Interleave A/B/A/B. Report fps for each arm, the spread per arm, and the **ratio B/A**.++If you have budget, add a third probe: arm B′ with inference running *as fast as possible*+rather than at 60 Hz. That is an upper bound, not the realistic load, and should be labelled as+such.++## What to report back++A short written report, in this shape:++| | value |+|---|---|+| GPU / driver / integrated or discrete | |+| Machine representative of the target? | |+| Inference p50 / p95 (DirectML) | |+| Inference p50 / p95 (CPU EP) | |+| Model input / output shapes as measured | |+| C8 fps — arm A (spread) | |+| C8 fps — arm B (spread) | |+| **Ratio B/A** | |+| Run admissible under §C.2? | |++Then, in prose:++- **the gate**: does the compositor still hold its budget with inference in the loop? Answer+ yes/no and show the arithmetic;+- anything that surprised you, and anything you could not measure;+- every run you discarded and why. A VOID run reported as VOID is a useful result; a VOID run+ reported as a number is a harmful one.++## What would change the answer++Flag these if you hit them:++- ops falling back to the CPU EP — that changes the latency picture entirely;+- DirectML failing to initialise on this GPU/driver;+- the fixture not being representative (resolution, frame rate) of a real recording;+- the compositor bench not reaching its documented ~126 fps ballpark on arm A, which would mean+ the machine is not comparable to the reference and the ratio is all you can offer.diff --git a/technical-documentation/engineering/webcam-segmentation.md b/technical-documentation/engineering/webcam-segmentation.mdnew file mode 100644index 000000000..22853e76d--- /dev/null+++ b/technical-documentation/engineering/webcam-segmentation.md@@ -0,0 +1,157 @@+# Webcam background segmentation++Where the AI cutout/blur/custom-background feature stands, the constraint that shapes it, and+the measurement that has to choose the next step.++Companion to [rendering-performance.md](rendering-performance.md), whose protocol §C.2 governs+every number quoted here.++## The principle at stake++Two decisions already in force bound this feature:++> **Compositing and encoding run in one native D3D11 engine**, shared by live preview and export.+> — [decisions.md](../architecture/decisions.md)++> A single `ID3D11Device`, zero CPU readback between stages.+> — [crates/README.md](../../crates/README.md)++The shader is the pixel SSOT. Preview and export are identical because they run *the same code*,+not because two implementations are kept in step by hand.++## What ships today++The camera is segmented by **MediaPipe SelfieSegmentation running in the renderer**+(`src/lib/ai-edition/webcamSegmentation.ts`), on Chromium's ANGLE device or the WASM CPU+fallback. What happens next differs by path:++| Path | Who composites the camera | How the mask travels |+|---|---|---|+| Preview | a DOM `<canvas>` over the native canvas | never leaves the renderer |+| Export | the shader, via `draw_video` | baked into a video, then decoded back |++So the layer has **two compositors**, and the export half round-trips the composite through a+codec. `NativeCompositorOverlay` withholds `webcamPath` from the native view while an effect is+active, so exactly one of them owns the layer at a time — but they remain two implementations.++### The known limitation++Transparent (cutout) **export** cannot work. The pre-rendered track is VP9-with-alpha in WebM,+and the native decode path is ffmpeg → NV12: `draw_video(&wy, &wuv)` binds a Y plane and a UV+plane, and there is no third plane. Alpha is dropped at decode, so the subject lands on an+opaque black rectangle. Blur and custom background are unaffected — their tracks are opaque, so+NV12 loses nothing.++This is structural, not a bug to patch: it is what "bake the composite" costs.++## The constraint that shapes any fix++The mask must reach the shader as a texture. That part is cheap and identical under every+option: `t0`/`t1` are the webcam NV12 and `t2` is the wallpaper/sprite slot, so **`t3` is free**,+and `fx.y`/`fx.z` are unused in mode 0 and can carry mode + intensity.++```hlsl+Texture2D<float> texMask : register(t3);++// ps_main, mode < 0.5, after the motion-blur accumulation:+float m = texMask.Sample(samp, uv_now); // 256x144, upscaled by the sampler+if (effect > 2.5) rgb = lerp(color.rgb, rgb, m); // custom background+else if (effect > 1.5) rgb = lerp(blur_bg(uv_now), rgb, m); // blur+else if (effect > 0.5) alpha = alpha * m; // cutout+```++What is *not* cheap is producing that mask on the device that consumes it.++- The compositor renders on its own `ID3D11Device` in the **main** process — feature level 11_1+ only (`d3d_windows.rs:75` bails on anything else), default adapter (`pAdapter = None`), and+ `SetMultithreadProtected(true)`.+- MediaPipe runs in the **renderer** process. No JS API exposes a shared DXGI handle from ANGLE,+ so zero-copy between the two is unreachable from that side.+- **DirectML binds to a D3D12 device, not D3D11.** "Run ONNX Runtime on the compositor's device"+ is therefore not a thing; it needs D3D11↔D3D12 shared-handle interop on a matching adapter LUID.++## Options++| | A — transport the mask | B — inference in the compositor | C — hand-written compute |+|---|---|---|---|+| Preview and export ISO | no, still two compositors | yes, same code | yes, same code |+| New native runtime | none | one | none |+| GPU interop | n/a | D3D11↔D3D12 (Windows) | none, same device |+| Main risk | mask lags the frame it masks | queue contention; packaging ×5 | porting a CNN to 3 shading languages |++**A** is cheap and fixes the alpha loss, but regresses something that works today: the preview+mask is currently perfectly aligned, because MediaPipe segments the very frame the canvas draws.+Sourcing it from a second decoder means pairing by PTS and accepting a lag, which shows on+moving edges.++**C** matches the repo's habit of writing GPU maths from scratch, but means porting a+MobileNet-class graph to HLSL *and* Metal *and* GLSL, with the numerical validation that implies.++**B** is the recommended direction — with the interop cost stated honestly above.++### The platform multiplier++This is the part that dominates B's cost, and it is not Windows-shaped:++| | Compositor | GPU inference | Interop needed |+|---|---|---|---|+| Windows | D3D11 | DirectML (D3D12) | D3D11↔D3D12 |+| macOS | Metal | MPSGraph / CoreML | none — same `MTLDevice` |+| Linux | Vulkan | ORT has no Vulkan EP | depends on the EP |++Linux is the weak leg. Note that **ggml with a Vulkan backend already ships in this app** —+whisper.cpp builds it for Windows and Linux, Metal for macOS arm64, with the Vulkan-SDK CI leg+already debugged (`.github/workflows/build-whisper-stt.yml`). An engine covering all three+platforms is therefore already packaged; what it lacks is a graph for this model, which would be+written once rather than three times. That trade deserves to be re-examined if ORT's Linux story+proves as thin as it looks.++## The measurement that decides++Do not build the interop before this is answered. The ledger already records the directly+analogous outcome:++> **Encoder pipelining** — *Implemented and measured: a loss on the target integrated GPU,+> because encode and composite contend for the same queue.*++Per-frame inference is the same shape of work. The decisive question is whether the target+integrated GPU has the headroom, not whether the copies can be elided — the mask is 256×144 R8,+36 KB, and copying it was never the cost.++### What to measure++1. **Isolated inference latency.** ms per inference of MediaPipe SelfieSegmentation *landscape*+ (256×144) on the target GPU, p50 and p95, warm.+2. **Contention.** The C8 bench with and without a concurrent 60 Hz inference load on the same+ GPU. This needs no integration: a second process hammering the GPU answers "is there+ headroom" without a line of compositor code.++### Protocol++Governed by [rendering-performance.md](rendering-performance.md) §C.2, which is not optional:++- one full warm-up sweep discarded before any reported run;+- `--repeat 3`, interleaved A/B/A/B arms;+- **spread gate: a run above 15 % declares itself VOID** and is not quotable;+- a quiet machine — the documented VOID example had ~40 browser and Electron processes live;+- **only within-run ratios transfer between machines, never absolute times.** The deliverable is+ a ratio, not a frame time.++```+crates\x.bat run --release -- --cfg C0..C8 --fixture fixture --repeat 3 --out out\+```++`x.bat` wraps vcvars + ffmpeg-on-PATH + cargo. Its hardcoded Visual Studio path may need+adjusting per machine — the file says so itself. A worktree also has no `crates/thirdparty/`+(gitignored); it must be copied from a full checkout before anything builds.++### The gate++If inference costs ~6 ms on a frame budget of ~8, the design is dead on the target hardware and+the interop question never arises — the answer is then A, or staying with a pre-render. Decide on+the ratio, then build.++## Appendix — brief for the measurement++A self-contained brief for whoever (or whatever) runs this on the test machine lives in+[webcam-segmentation-bench-brief.md](webcam-segmentation-bench-brief.md).