Skip to content

perf(windows-camera): reuse upload texture and media buffers - #3134

Open
kyzer1023 wants to merge 12 commits into
CapSoftware:mainfrom
kyzer1023:perf/windows-camera-texture-reuse
Open

kyzer1023 wants to merge 12 commits into
CapSoftware:mainfrom
kyzer1023:perf/windows-camera-texture-reuse

Conversation

@kyzer1023

@kyzer1023 kyzer1023 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

The Windows native camera path allocated twice for every frame. setup_camera copied each sample into a new MFCreateMemoryBuffer, and upload_mf_buffer_to_texture then created a new ID3D11Texture2D from that copy with CreateTexture2D. This change reuses both.

  • output_pipeline/win.rs: CameraBuffers now keeps one cached upload texture. It is a D3D11_USAGE_DYNAMIC texture filled with Map(WRITE_DISCARD), copied row by row with separate source and mapped row pitches (both planes for NV12). It is recreated only when width, height, DXGI format or device change. If the driver refuses the dynamic texture, it falls back to a cached D3D11_USAGE_DEFAULT texture updated with UpdateSubresource. One texture is enough because the encoder's VideoProcessor copies the input into its own texture with CopyResource on the same immediate context before it asks for the next frame, and WRITE_DISCARD keeps the memory that queued GPU work still reads.
  • The upload now returns an error when the camera buffer is too small for the texture (row pitch or total length), instead of passing it to CreateTexture2D.
  • feeds/camera.rs: the native frame path keeps a pool of up to 3 media buffers. A buffer is reused only when the pool holds the only reference to it, it is large enough and it is not poisoned. Otherwise a new buffer is allocated as before. The current length is set on every frame, and a buffer that fails a write is dropped from the pool. The preview FFmpeg copy is unchanged.
  • examples/windows-camera-upload-benchmark.rs: a Windows only example that captures real webcam frames and replays them through an inline copy of the previous path and through the new one, plus a GPU readback check. Capture only accepts top-down YUYV, NV12, RGB32 and ARGB frames, the modes both paths can replay. It is a no-op main on other platforms.

Benchmark

Per frame cost of the camera upload path (media buffer copy plus texture upload), release build.

Mode Version Median ms p95 ms Mean ms CPU s
Submit only before 0.790 1.064 0.831 1.250
Submit only after 0.215 0.282 0.233 0.406
Wait for GPU each frame before 0.810 1.079 0.845 1.219
Wait for GPU each frame after 0.337 0.504 0.396 0.609

Speedup, submit only: 3.67x median, 3.77x p95, 3.57x mean, 3.08x CPU.
Speedup, waiting for GPU: 2.40x median, 2.14x p95, 2.13x mean, 2.00x CPU.

CPU s is total process CPU time across the 1500 timed frames of each row. An earlier run with a separate harness that built the original library code for the before side gave similar numbers (3.97x and 2.60x median).

  • Hardware: AMD Ryzen 7 8745HS, AMD Radeon 780M (driver 32.0.21030.2001), Windows 11, rustc 1.88.0.
  • Webcam: USB Camera (VID 0C45, PID 6340) at 1920x1080, 30 fps, NV12. This webcam does not offer YUYV at 1080p; its largest YUYV modes are 1280x720 and 1280x960.
  • Method: 300 real frames captured once and replayed, 30 untimed warmup frames per repeat, 5 repeats per side, 1500 samples per row. Submit only flushes after each frame. Wait for GPU ends an event query and waits for it after each frame.

How to rerun

cargo build --release -p cap-recording --example windows-camera-upload-benchmark
target\release\examples\windows-camera-upload-benchmark.exe capture frames.bin "USB Camera"
target\release\examples\windows-camera-upload-benchmark.exe both frames.bin
target\release\examples\windows-camera-upload-benchmark.exe validate

The camera name argument is optional and matches by substring; without it the first camera is used. before and after run one side each.

Correctness

  • validate uploads 90 synthetic frames through upload_mf_buffer_to_texture with one shared CameraBuffers and checks each GPU readback byte for byte: YUYV, UYVY, NV12 (both planes), RGB32 and ARGB, padded rows, bottom up flips, three frames in a row per format, and size and format changes. It passes on the dynamic path. The same checks also passed in a local build with the UpdateSubresource fallback forced.
  • A live check with the webcam held 3 native frames while 30 more were captured. The held frames stayed byte identical, and after they were released the pool reused one buffer.

AMD caveat

WindowsCameraMuxer routes AMD GPUs to the FFmpeg software camera encoder, which does not call upload_mf_buffer_to_texture. So on that path the texture upload gain applies to hardware encoding on Intel and NVIDIA, not to AMD machines like the one measured here. The numbers above measure the upload path in isolation, not a full recording. The segmented camera encoder has no AMD check and uses the upload on every vendor. The media buffer pool applies everywhere a native camera sender is attached.

Tests

  • cargo fmt --all and cargo clippy -p cap-recording --all-targets are clean apart from two existing warnings (cursor.rs unused import, capture_pipeline.rs items after test module).
  • cargo test -p cap-recording: library tests 714 passed, 4 ignored.
  • instant_mode_scenarios has 3 failures (many_segments_50_plus_concatenate_correctly, segment_callback_receives_events_during_encoding, segment_duration_100ms_produces_many_segments). They fail the same way on unmodified main on this machine and go through the FFmpeg AMF encoder, which this change does not touch.
  • The oop_muxer tests could not find cap-muxer.exe because this run used a shared target dir outside the worktree.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge, with both prior findings addressed and no new findings.

Summary

Reuses Windows camera media buffers and upload textures to reduce per-frame allocations.

  • Camera frame uploads reuse a matching Direct3D texture.
  • Windows camera frames reuse up to three media buffers.
  • Wayland display queries wait longer for busy compositors.
  • Linux reliability runs use the active desktop setup.

No code changed since the previous review. Both prior findings are addressed: capture excludes unsupported benchmark modes and rejects bottom-up frames before writing; the benchmark’s opening narrative comments are removed. No new findings or repository-rule violations remain.

Reviews (10) · Last reviewed commit: "ci: retrigger after macos-intel native-d..." · Reviewed by Greptile

@vercel

vercel Bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

@kyzer1023 is attempting to deploy a commit to the Cap Software Inc Team on Vercel.

A member of the Team first needs to authorize it.

@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai review

@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai review

@kyzer1023 kyzer1023 changed the title Reuse Windows camera upload texture and media buffers perf(windows-camera): reuse upload texture and media buffers Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai review

Copy link
Copy Markdown
Contributor Author

@greptileai review

Copy link
Copy Markdown
Contributor Author

@greptileai review

@richiemcilroy

Copy link
Copy Markdown
Member

Thanks, the reuse looks right. One thing before we take it: RGB24/BGR24 cameras now hit the new size check and error out, so their recordings stop where they used to complete (with bad colours). Can you convert those to BGRA before the upload instead, so they're actually fixed rather than failing?

@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai review

@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai review

@kyzer1023

Copy link
Copy Markdown
Contributor Author

@richiemcilroy e0eaaec (fix in ff7c575): RGB24/BGR24 camera frames now convert to BGRA before upload instead of hard-failing. Greptile 5/5. Windows CI green: tests 1–4, clippy, GPUI build, sync-tests, real-capture (windows-2022), window discovery. Earlier windows sync-tests failure was an unrelated rendering transition flake; only macos-26-intel GPUI build still queued.

@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants