Skip to content

fix(rendering): key the shader cache per adapter - #4496

Merged
richiemcilroy merged 4 commits into
CapSoftware:mainfrom
kyzer1023:fix/shader-cache-per-adapter
Nov 24, 2026
Merged

richiemcilroy merged 4 commits into
CapSoftware:mainfrom
kyzer1023:fix/shader-cache-per-adapter

Conversation

@kyzer1023

@kyzer1023 kyzer1023 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why

CompositeVideoFramePipeline stored one shared shader_cache.bin for every GPU adapter. On a dual-GPU system (for example a laptop switching between NVIDIA and Intel Vulkan ICDs), wgpu rejects a cache blob created for another device, and fallback: true does not cover that mismatch—leaving the composite pipeline invalid with "Pipeline cache validation failed: The cache data was created for a different device" and subsequent Device::poll panics.

What changed

  • Scoped to crates/rendering/src/composite_frame.rs only.
  • Key the Vulkan pipeline cache filename per adapter using wgpu::util::pipeline_cache_key with vendor and device IDs read from the live Vulkan physical device (wgpu_pipeline_cache_vulkan_<vendor>_<device>.bin).
  • Wrap cache creation in a validation error scope: if the current device rejects a blob (foreign or corrupt), drop it, remove the bad file, and create a fresh empty cache, writing updates via a temporary file.
  • Leave the legacy shared shader_cache.bin unread so existing poisoned installs start clean. Metal and DX12 do not enable PIPELINE_CACHE in this repo and continue to skip caching.
  • Added shader_cache_tests in crates/rendering/src/composite_frame.rs covering foreign-header and corrupt-blob recovery.

Verification

  • Unit tests (lavapipe): cargo test -p cap-rendering --lib shader_cache_tests -- --test-threads=1 passed (2/2). Before the fix, a foreign header rendered clear color [0, 0, 0, 0] with the device-mismatch error; after the fix, both foreign-header and corrupt-blob tests render [255, 0, 0, 255], replace the bad file, and log zero uncaptured wgpu errors.
  • Dual-GPU hardware verification (Linux Mint 22.3, NVIDIA GeForce MX150 0x10de/0x1d10 + Intel UHD Graphics 620 0x8086/0x5917, opening a 15 s 1920×1080 .cap in the GPUI editor):
    1. NVIDIA MX150 run: Rendered 480 frames (60.0 fps, total_skipped=0); created wgpu_pipeline_cache_vulkan_4318_7440.bin (61,566 bytes); legacy shader_cache.bin untouched; zero wgpu/poll errors.
    2. Switch to Intel UHD 620 in the same data dir (with MX150 cache + NVIDIA-poisoned legacy shader_cache.bin present): Rendered 537 frames (59.8 fps, total_skipped=0); created wgpu_pipeline_cache_vulkan_32902_22807.bin (185,856 bytes) alongside unchanged MX150 cache; legacy shader_cache.bin ignored; zero Device::poll panics or uncaptured wgpu errors.
    3. Live poisoned-file recovery: Overwrote wgpu_pipeline_cache_vulkan_32902_22807.bin directly with the MX150 blob and reopened the editor on Intel UHD 620 -> logged WARN shader cache rejected for this adapter; starting a new one, rendered 538 frames (59.9 fps, total_skipped=0), zero Device::poll panics or uncaptured errors, and replaced the file with a fresh valid Intel cache (55,536 bytes).

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no blocking issues were found.

Summary

Keys the shader cache by the active Vulkan adapter and retries with an empty cache when saved bytes are rejected.

  • Each Vulkan adapter uses its own shader cache, and rejected data is replaced.

The previous test finding is fixed. No new actionable issues were found.

Reviews (3) · Last reviewed commit: "Merge branch 'main' into fix/shader-cach..." · Reviewed by Greptile

Key the wgpu pipeline cache by adapter and drop a blob this device rejects, so a cache written by another GPU cannot invalidate the composite pipeline.
@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

@vercel

vercel Bot commented Nov 23, 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.

Comment thread crates/rendering/src/composite_frame.rs Outdated
Comment on lines +382 to +383
let stored = std::fs::read(&path).unwrap_or_default();
assert_ne!(stored, foreign_header());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing cache files pass tests

The recovery test passes even if save_pipeline_cache never writes anything. Once the foreign cache is removed, unwrap_or_default() turns the missing file into empty bytes, which passes assert_ne!. The corrupt-cache case only checks drawing. Require the file to exist after both cases, then reload its contents to check that recovery saved a usable cache. This would catch changes that silently stop cache reuse.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/rendering/src/composite_frame.rs
Line: 382-383

Comment:
**Missing cache files pass tests**

The recovery test passes even if `save_pipeline_cache` never writes anything. Once the foreign cache is removed, `unwrap_or_default()` turns the missing file into empty bytes, which passes `assert_ne!`. The corrupt-cache case only checks drawing. Require the file to exist after both cases, then reload its contents to check that recovery saved a usable cache. This would catch changes that silently stop cache reuse.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ee1b2b9a. Both recovery cases now assert std::fs::read succeeds with non-empty bytes that differ from the rejected foreign/corrupt blob, then reload that saved file through CompositeVideoFramePipeline::new, draw a frame, and check that reload logs neither a cache rejection nor an uncaptured wgpu error while keeping the cache file on disk (when the adapter exposes Features::PIPELINE_CACHE).

The recovery test now reads the cache file after a foreign blob and a corrupt blob, then loads those bytes again and draws.
@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 23, 2026
@richiemcilroy

Copy link
Copy Markdown
Member

@greptileai review

@richiemcilroy
richiemcilroy merged commit d9adc10 into CapSoftware:main Nov 24, 2026
72 checks passed
kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 24, 2026
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