[PR #7212] [MERGED] DX12: compile shaders once per game folder, not on every start #7412

Closed
opened 2026-11-03 06:39:12 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7212
Author: @ludovic111
Created: 11/2/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @ludovic111

Base: main ← Head: perf/dx12-shader-disk-cache


📝 Commits (1)

  • 8bba5bd DX12: keep compiled shaders on disk between runs

📊 Changes

5 files changed (+329 additions, -7 deletions)

View changed files

📝 crates/horizon-client-render/src/gfx.rs (+12 -0)
📝 third_party/wgpu-hal/src/dx12/device.rs (+22 -7)
➕ third_party/wgpu-hal/src/dx12/disk_cache.rs (+275 -0)
📝 third_party/wgpu-hal/src/dx12/mod.rs (+11 -0)
📝 third_party/wgpu-hal/src/dx12/shader_compilation.rs (+9 -0)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

On DirectX 12, every start spends seconds compiling shaders: about 2.6–3.7 s with FXC, the compiler Windows ships and the one wgpu falls back to. DX12 runs when Vulkan doesn't work or when the player picks it in the graphics settings. With this PR, those shaders are compiled only once per game folder; every later start reads them back from disk.

Measured

Measured on a GitHub-hosted windows-latest runner with the WARP adapter. The measuring tool is the boot_pipelines example on the measurement branch perf/dx12-fxc-measure, which is not for merging. It times Renderer::new (65 shader modules plus their pipelines), each run in a new process. Runs 41439258623 and 41447442462.

first start in a game folder later starts cache file
FXC, before 2,511–2,856 ms (every start) same —
FXC, this PR 3,325–3,671 ms 216–226 ms 350 KB
DXC (dxcompiler.dll), before 1,029–1,068 ms (every start) same —
DXC, this PR 1,421–1,485 ms 195–206 ms 533 KB
static DXC (static-dxc feature), not taken 749–891 ms (every start) same —

The two runs ran on different runners, so the cold numbers differ by about 0.5–0.8 s between runs. Within the second run, a warm start saves 3.1 s with FXC and 1.2 s with DXC. The ~200 ms left is naga's HLSL output and pipeline creation.

Why not static DXC: it only cuts every start to ~0.8 s, and it costs size. horizon-client.exe grows from 74.9 MB to 97.4 MB (+22.6 MB), and the zipped exe from 27.9 MB to 36.4 MB (+8.5 MB). The cache gets later starts to ~0.2 s with no size cost, so this PR doesn't change the build.

How

  • third_party/wgpu-hal/src/dx12/disk_cache.rs (Horizon's change to the vendored wgpu-hal, like its other patches): one file, <game home>/cache/dx12-shaders.bin. On a miss in wgpu-hal's in-memory shader cache, Device::load_shader looks there before it compiles. A shader it does compile is appended. The cached bytecode goes back in as CompiledShader::Precompiled.
  • The key is a 128-bit FNV-1a hash of the compiler (fxc / dxc / static-dxc), shader model, stage, entry point and the HLSL naga generated after overrides. A naga, wgpu, shader or compiler change is a new key, so nothing has to be invalidated by hand.
  • Each record has a 64-bit checksum. Loading stops at the first short or mismatched record (a run killed mid-write, two games at once), and the next append overwrites from there. A file over 64 MB is started over.
  • gfx::instance_descriptor names the file when the DX12 backend is in the instance. Nothing changes on macOS, Linux or Vulkan.

Tested

  • The warm runs above create every pipeline from cached bytecode on a real D3D12 device (WARP).
  • disk_cache unit tests (read back in the next run, each key part matters, torn tail and flipped byte, foreign file). third_party/wgpu-hal is outside the workspace, so I ran them as a standalone rustc --test build on macOS: 4 passed.
  • cargo check --target x86_64-pc-windows-msvc --features dx12 --lib --tests and clippy on third_party/wgpu-hal: clean. PR CI has no Windows build. Run 41447442462 built horizon-client-render and this wgpu-hal for Windows from a merge of this head (perf/dx12-fxc-measure at ca142e09) and ran the cold/warm table above with it; the nightly's Windows build covers the game binary.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/7212 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/dx12-shader-disk-cache` --- ### 📝 Commits (1) - [`8bba5bd`](https://github.com/horizon-rust/horizon/commit/8bba5bd368fbb4ea7a4c0e75c8a02030d4c88f1c) DX12: keep compiled shaders on disk between runs ### 📊 Changes **5 files changed** (+329 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-render/src/gfx.rs` (+12 -0) 📝 `third_party/wgpu-hal/src/dx12/device.rs` (+22 -7) ➕ `third_party/wgpu-hal/src/dx12/disk_cache.rs` (+275 -0) 📝 `third_party/wgpu-hal/src/dx12/mod.rs` (+11 -0) 📝 `third_party/wgpu-hal/src/dx12/shader_compilation.rs` (+9 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. On DirectX 12, every start spends seconds compiling shaders: about 2.6–3.7 s with FXC, the compiler Windows ships and the one wgpu falls back to. DX12 runs when Vulkan doesn't work or when the player picks it in the graphics settings. With this PR, those shaders are compiled only once per game folder; every later start reads them back from disk. ## Measured Measured on a GitHub-hosted `windows-latest` runner with the WARP adapter. The measuring tool is the `boot_pipelines` example on the measurement branch `perf/dx12-fxc-measure`, which is not for merging. It times `Renderer::new` (65 shader modules plus their pipelines), each run in a new process. Runs 41439258623 and 41447442462. | | first start in a game folder | later starts | cache file | |---|---|---|---| | FXC, before | 2,511–2,856 ms (every start) | same | — | | FXC, this PR | 3,325–3,671 ms | **216–226 ms** | 350 KB | | DXC (dxcompiler.dll), before | 1,029–1,068 ms (every start) | same | — | | DXC, this PR | 1,421–1,485 ms | **195–206 ms** | 533 KB | | static DXC (`static-dxc` feature), not taken | 749–891 ms (every start) | same | — | The two runs ran on different runners, so the cold numbers differ by about 0.5–0.8 s between runs. Within the second run, a warm start saves 3.1 s with FXC and 1.2 s with DXC. The ~200 ms left is naga's HLSL output and pipeline creation. **Why not static DXC**: it only cuts every start to ~0.8 s, and it costs size. `horizon-client.exe` grows from 74.9 MB to 97.4 MB (+22.6 MB), and the zipped exe from 27.9 MB to 36.4 MB (+8.5 MB). The cache gets later starts to ~0.2 s with no size cost, so this PR doesn't change the build. ## How - `third_party/wgpu-hal/src/dx12/disk_cache.rs` (Horizon's change to the vendored wgpu-hal, like its other patches): one file, `<game home>/cache/dx12-shaders.bin`. On a miss in wgpu-hal's in-memory shader cache, `Device::load_shader` looks there before it compiles. A shader it does compile is appended. The cached bytecode goes back in as `CompiledShader::Precompiled`. - The key is a 128-bit FNV-1a hash of the compiler (fxc / dxc / static-dxc), shader model, stage, entry point and the HLSL naga generated after overrides. A naga, wgpu, shader or compiler change is a new key, so nothing has to be invalidated by hand. - Each record has a 64-bit checksum. Loading stops at the first short or mismatched record (a run killed mid-write, two games at once), and the next append overwrites from there. A file over 64 MB is started over. - `gfx::instance_descriptor` names the file when the DX12 backend is in the instance. Nothing changes on macOS, Linux or Vulkan. ## Tested - The warm runs above create every pipeline from cached bytecode on a real D3D12 device (WARP). - `disk_cache` unit tests (read back in the next run, each key part matters, torn tail and flipped byte, foreign file). `third_party/wgpu-hal` is outside the workspace, so I ran them as a standalone `rustc --test` build on macOS: 4 passed. - `cargo check --target x86_64-pc-windows-msvc --features dx12 --lib --tests` and clippy on `third_party/wgpu-hal`: clean. PR CI has no Windows build. Run 41447442462 built `horizon-client-render` and this wgpu-hal for Windows from a merge of this head (`perf/dx12-fxc-measure` at ca142e09) and ran the cold/warm table above with it; the nightly's Windows build covers the game binary. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:39:12 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#7412
No description provided.