From d1938879c11f61b12e399cabcf8ef0d22427f34a Mon Sep 17 00:00:00 2001 From: Saurabh Suresh Date: Thu, 26 Nov 2026 18:34:58 +0400 Subject: [PATCH 1/2] feat(gpu): 64-bit atomic-lock rounds on by default under Wine The rounds from #257 are now on wherever the atomic locks are, and VICTORY_ATOMIC_LOCK_ROUNDS=0 goes back to the turns alone, as asked on #240. In the game they took a world frame from 7.9 to 2.5 s on an M2 Max (sys-skye), from 5.29 to 0.71 s on an M4 Pro (evan-kolberg, same binary) and from 2.9 to 0.88 s on a 16 GB M4 (Stefa008), and the GPU resets and queue recoveries of the turns stopped. Test: lock_rounds_are_on_with_the_locks_unless_turned_off. docs/macos.md describes the rounds as the default, with those numbers. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/shader_cache.rs | 26 +++++++++++++++++++++++--- docs/macos.md | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/victory-gpu/src/shader_cache.rs b/crates/victory-gpu/src/shader_cache.rs index 6ec4da76..28a1e765 100644 --- a/crates/victory-gpu/src/shader_cache.rs +++ b/crates/victory-gpu/src/shader_cache.rs @@ -102,15 +102,24 @@ fn atomic_locks() -> bool { /// Whether the lanes of a wave first try their 64-bit atomics' locks in /// rounds before the lanes still not done take turns (`emit`, "Atomic -/// locks"; #240): opt-in while it is measured, `VICTORY_ATOMIC_LOCK_ROUNDS=1`. -/// Only with [`atomic_locks`]. +/// locks"; #240): wherever [`atomic_locks`] are on. In the game the rounds +/// took a world frame from 7.9 to 2.5 s on an M2 Max, from 5.29 to 0.71 s +/// on an M4 Pro and from 2.9 to 0.88 s on an M4, and left no GPU resets. +/// `VICTORY_ATOMIC_LOCK_ROUNDS=0` goes back to the turns alone. fn atomic_lock_rounds() -> bool { static ROUNDS: OnceLock = OnceLock::new(); *ROUNDS.get_or_init(|| { - atomic_locks() && std::env::var("VICTORY_ATOMIC_LOCK_ROUNDS").as_deref() == Ok("1") + let var = std::env::var("VICTORY_ATOMIC_LOCK_ROUNDS").ok(); + rounds_on(atomic_locks(), var.as_deref()) }) } +/// The rounds, given whether the locks are on and +/// `VICTORY_ATOMIC_LOCK_ROUNDS`: on with the locks unless it is `0`. +fn rounds_on(locks: bool, var: Option<&str>) -> bool { + locks && var != Some("0") +} + /// Whether compute shaders' biased samples run as scaled gradient samples: /// under Wine, whose D3DMetal ignores the bias there (#30). /// `VICTORY_BIAS_AS_GRADIENTS=0` or `1` overrides. @@ -508,6 +517,17 @@ fn decode(bytes: &[u8]) -> Option> { mod tests { use super::*; + /// The rounds are on wherever the locks are, `VICTORY_ATOMIC_LOCK_ROUNDS=0` + /// turns them off, and without the locks there are none (#240). + #[test] + fn lock_rounds_are_on_with_the_locks_unless_turned_off() { + assert!(rounds_on(true, None), "on by default with the locks"); + assert!(rounds_on(true, Some("1")), "the old opt-in still works"); + assert!(!rounds_on(true, Some("0")), "0 goes back to the turns"); + assert!(!rounds_on(false, None), "no rounds without the locks"); + assert!(!rounds_on(false, Some("1")), "nor when asked for"); + } + fn shader() -> HostShader { HostShader { stage: HwStage::Ps, diff --git a/docs/macos.md b/docs/macos.md index 4faba0f9..88942ec0 100644 --- a/docs/macos.md +++ b/docs/macos.md @@ -150,7 +150,7 @@ This was the "signed zero in minimums" difference listed here before. Each `v_mi The merge of VIctory's changes up to 70564ae on 25 November 2026 brought new shader tests, and three of them find real differences in D3DMetal 4.0 beta 3 on an M4. The tests skip only those checks under Wine, so the rest of each test still runs. -- 64-bit image atomics. D3DMetal reports `AtomicInt64OnTypedResourceSupported` and `AtomicInt64OnDescriptorHeapResourceSupported`, but its 64-bit atomics in `image_atomics_of_64_bits` leave every texel at zero. The game's alpha prepasses use them. Under Wine the translator now emulates them (`Options::atomic_locks`, the module notes' "Atomic locks", `VICTORY_ATOMIC_LOCKS=0` or `1` to override): the lanes of a wave take turns in a loop over lane indices, and in its turn a lane takes a 32-bit lock word, reads the texel, writes the result and frees the lock. Two simpler forms failed under D3DMetal and are worth knowing about. Lanes of one wave spinning on each other's locks never finish, and `WaveIsFirstLane` and `WaveReadLaneFirst` inside a divergent loop return other lanes' values. The spin is bounded at 65536 tries so a fault can never hang the GPU. On an M2 Pro these locks cost about 6 s of a 7.4 s world frame (#240), almost all of it in the alpha prepasses' 64-bit `umax`. `VICTORY_ATOMIC_LOCK_ROUNDS=1` (opt-in while it is measured in the game) makes every lane of a wave first try its lock once in each of four rounds, all at once, so lanes whose locks differ do not wait for each other; only lanes still not done take turns, and a wave whose lanes all finished skips the turns. No lane waits on a lock inside a round, so lanes of one wave still never wait for each other's locks. On an M5 Pro with D3DMetal 4.0 beta 3 a wave's 64-bit `umax` on distinct texels took 29 µs in turns and 6 µs in rounds (`atomic_lock_rounds_timing`, ignored, run under Wine). In pixel programs the rounds leave helper lanes out: their atomics are discarded, so they could never take a lock, and in their turn they would spin to the bound. +- 64-bit image atomics. D3DMetal reports `AtomicInt64OnTypedResourceSupported` and `AtomicInt64OnDescriptorHeapResourceSupported`, but its 64-bit atomics in `image_atomics_of_64_bits` leave every texel at zero. The game's alpha prepasses use them. Under Wine the translator now emulates them (`Options::atomic_locks`, the module notes' "Atomic locks", `VICTORY_ATOMIC_LOCKS=0` or `1` to override): the lanes of a wave take turns in a loop over lane indices, and in its turn a lane takes a 32-bit lock word, reads the texel, writes the result and frees the lock. Two simpler forms failed under D3DMetal and are worth knowing about. Lanes of one wave spinning on each other's locks never finish, and `WaveIsFirstLane` and `WaveReadLaneFirst` inside a divergent loop return other lanes' values. The spin is bounded at 65536 tries so a fault can never hang the GPU. On an M2 Pro these locks cost about 6 s of a 7.4 s world frame (#240), almost all of it in the alpha prepasses' 64-bit `umax`. The rounds, on by default wherever the locks are (`VICTORY_ATOMIC_LOCK_ROUNDS=0` goes back to the turns alone), make every lane of a wave first try its lock once in each of four rounds, all at once, so lanes whose locks differ do not wait for each other; only lanes still not done take turns, and a wave whose lanes all finished skips the turns. No lane waits on a lock inside a round, so lanes of one wave still never wait for each other's locks. On an M5 Pro with D3DMetal 4.0 beta 3 a wave's 64-bit `umax` on distinct texels took 29 µs in turns and 6 µs in rounds (`atomic_lock_rounds_timing`, ignored, run under Wine). In the game the rounds took a world frame from 7.9 to 2.5 s on an M2 Max (sys-skye), from 5.29 to 0.71 s on an M4 Pro (evan-kolberg, the same binary with only the switch changed) and from 2.9 to 0.88 s on a 16 GB M4 (Stefa008), and the GPU resets and queue recoveries of the turns stopped (#240). In pixel programs the rounds leave helper lanes out: their atomics are discarded, so they could never take a lock, and in their turn they would spin to the bound. - Sample bias in compute programs. D3DMetal ignores the bias of a sample in a compute shader: a bias of 1 reads level 1 where the console reads level 2. Under Wine the translator now runs those samples as gradient samples with the quad gradients scaled by `2^bias` (`Options::bias_as_gradients`, found by nickorlabs in #30; `VICTORY_BIAS_AS_GRADIENTS=0` or `1` overrides). The bias cases in `image_addresses_of_16_bits_and_clamps` run under Wine again and pass. Pixel shaders keep the plain biased sample. - 24-bit depth. Apple GPUs have no 24-bit depth format, so D3DMetal keeps it as 32-bit float and rounds down when it stores 24 bits. The depth test clears to 0.75, where both roundings agree (PR #6). - Depth bounds. D3DMetal reports `DepthBoundsTestSupported` and does not apply the test (Metal has none): in the merged VIctory test a draw whose bounds exclude the stored depth still writes. Under Wine `Host::depth_bounds_supported` is false, so pipelines are made without bounds and one warning says so, which has the same effect and keeps the plain pipeline states. The test follows the flag. From 9536976c50faa5321b7db599c0fb1eb17a70763e Mon Sep 17 00:00:00 2001 From: Pragyaan Gaur Date: Thu, 26 Nov 2026 20:46:36 +0530 Subject: [PATCH 2/2] Keep one paragraph for the 64-bit atomics in the docs --- docs/macos.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/macos.md b/docs/macos.md index c7bee195..5dd0a3eb 100644 --- a/docs/macos.md +++ b/docs/macos.md @@ -150,8 +150,7 @@ This was the "signed zero in minimums" difference listed here before. Each `v_mi The merge of VIctory's changes up to 70564ae on 25 November 2026 brought new shader tests, and three of them find real differences in D3DMetal 4.0 beta 3 on an M4. The tests skip only those checks under Wine, so the rest of each test still runs. -- 64-bit image atomics. D3DMetal reports `AtomicInt64OnTypedResourceSupported` and `AtomicInt64OnDescriptorHeapResourceSupported`, but its 64-bit atomics in `image_atomics_of_64_bits` leave every texel at zero. The game's alpha prepasses use them. Under Wine the translator now emulates them (`Options::atomic_locks`, the module notes' "Atomic locks", `VICTORY_ATOMIC_LOCKS=0` or `1` to override): the lanes of a wave take turns in a loop over lane indices, and in its turn a lane takes a 32-bit lock word, reads the texel, writes the result and frees the lock. Two simpler forms failed under D3DMetal and are worth knowing about. Lanes of one wave spinning on each other's locks never finish, and `WaveIsFirstLane` and `WaveReadLaneFirst` inside a divergent loop return other lanes' values. The spin is bounded at 65536 tries so a fault can never hang the GPU. On an M2 Pro these locks cost about 6 s of a 7.4 s world frame (#240), almost all of it in the alpha prepasses' 64-bit `umax`. The rounds, on by default wherever the locks are (`VICTORY_ATOMIC_LOCK_ROUNDS=0` goes back to the turns alone), make every lane of a wave first try its lock once in each of four rounds, all at once, so lanes whose locks differ do not wait for each other; only lanes still not done take turns, and a wave whose lanes all finished skips the turns. No lane waits on a lock inside a round, so lanes of one wave still never wait for each other's locks. On an M5 Pro with D3DMetal 4.0 beta 3 a wave's 64-bit `umax` on distinct texels took 29 µs in turns and 6 µs in rounds (`atomic_lock_rounds_timing`, ignored, run under Wine). In the game the rounds took a world frame from 7.9 to 2.5 s on an M2 Max (sys-skye), from 5.29 to 0.71 s on an M4 Pro (evan-kolberg, the same binary with only the switch changed) and from 2.9 to 0.88 s on a 16 GB M4 (Stefa008), and the GPU resets and queue recoveries of the turns stopped (#240). In pixel programs the rounds leave helper lanes out: their atomics are discarded, so they could never take a lock, and in their turn they would spin to the bound. -- 64-bit image atomics. D3DMetal reports `AtomicInt64OnTypedResourceSupported` and `AtomicInt64OnDescriptorHeapResourceSupported`, but its 64-bit atomics in `image_atomics_of_64_bits` leave every texel at zero. The game's alpha prepasses use them. Under Wine the translator now emulates them (`Options::atomic_locks`, the module notes' "Atomic locks", `VICTORY_ATOMIC_LOCKS=0` or `1` to override): the lanes of a wave take turns in a loop over lane indices, and in its turn a lane takes a 32-bit lock word, reads the texel, writes the result and frees the lock. Two simpler forms failed under D3DMetal and are worth knowing about. Lanes of one wave spinning on each other's locks never finish, and `WaveIsFirstLane` and `WaveReadLaneFirst` inside a divergent loop return other lanes' values. The spin is bounded, so a lock nobody frees can never hang the GPU: 2^22 tries in compute and mesh shaders, which macOS never ends for running long, and 65536 in pixel shaders, whose draws macOS ends after about 50 ms. A lane past the bound goes on without the lock and can lose its update, which 65536 tries allowed in compute under heavy contention (2,048 groups adding into one texel lost adds in 2 of 100 runs on an M5 Pro, #240). A shader the GPU ends while one of its lanes holds a lock never frees it, so after a submission the GPU ended VIctory frees every lock. On an M2 Pro these locks cost about 6 s of a 7.4 s world frame (#240), almost all of it in the alpha prepasses' 64-bit `umax`. `VICTORY_ATOMIC_LOCK_ROUNDS=1` (opt-in while it is measured in the game) makes every lane of a wave first try its lock once in each of four rounds, all at once, so lanes whose locks differ do not wait for each other; only lanes still not done take turns, and a wave whose lanes all finished skips the turns. No lane waits on a lock inside a round, so lanes of one wave still never wait for each other's locks. On an M5 Pro with D3DMetal 4.0 beta 3 a wave's 64-bit `umax` on distinct texels took 29 µs in turns and 6 µs in rounds (`atomic_lock_rounds_timing`, ignored, run under Wine). In pixel programs the rounds leave helper lanes out: their atomics are discarded, so they could never take a lock, and in their turn they would spin to the bound. +- 64-bit image atomics. D3DMetal reports `AtomicInt64OnTypedResourceSupported` and `AtomicInt64OnDescriptorHeapResourceSupported`, but its 64-bit atomics in `image_atomics_of_64_bits` leave every texel at zero. The game's alpha prepasses use them. Under Wine the translator now emulates them (`Options::atomic_locks`, the module notes' "Atomic locks", `VICTORY_ATOMIC_LOCKS=0` or `1` to override): the lanes of a wave take turns in a loop over lane indices, and in its turn a lane takes a 32-bit lock word, reads the texel, writes the result and frees the lock. Two simpler forms failed under D3DMetal and are worth knowing about. Lanes of one wave spinning on each other's locks never finish, and `WaveIsFirstLane` and `WaveReadLaneFirst` inside a divergent loop return other lanes' values. The spin is bounded, so a lock nobody frees can never hang the GPU: 2^22 tries in compute and mesh shaders, which macOS never ends for running long, and 65536 in pixel shaders, whose draws macOS ends after about 50 ms. A lane past the bound goes on without the lock and can lose its update, which 65536 tries allowed in compute under heavy contention (2,048 groups adding into one texel lost adds in 2 of 100 runs on an M5 Pro, #240). A shader the GPU ends while one of its lanes holds a lock never frees it, so after a submission the GPU ended VIctory frees every lock. On an M2 Pro these locks cost about 6 s of a 7.4 s world frame (#240), almost all of it in the alpha prepasses' 64-bit `umax`. The rounds, on by default wherever the locks are (`VICTORY_ATOMIC_LOCK_ROUNDS=0` goes back to the turns alone), make every lane of a wave first try its lock once in each of four rounds, all at once, so lanes whose locks differ do not wait for each other; only lanes still not done take turns, and a wave whose lanes all finished skips the turns. No lane waits on a lock inside a round, so lanes of one wave still never wait for each other's locks. On an M5 Pro with D3DMetal 4.0 beta 3 a wave's 64-bit `umax` on distinct texels took 29 µs in turns and 6 µs in rounds (`atomic_lock_rounds_timing`, ignored, run under Wine). In the game the rounds took a world frame from 7.9 to 2.5 s on an M2 Max (sys-skye), from 5.29 to 0.71 s on an M4 Pro (evan-kolberg, the same binary with only the switch changed) and from 2.9 to 0.88 s on a 16 GB M4 (Stefa008), and the GPU resets and queue recoveries of the turns stopped (#240). In pixel programs the rounds leave helper lanes out: their atomics are discarded, so they could never take a lock, and in their turn they would spin to the bound. - Sample bias in compute programs. D3DMetal ignores the bias of a sample in a compute shader: a bias of 1 reads level 1 where the console reads level 2. Under Wine the translator now runs those samples as gradient samples with the quad gradients scaled by `2^bias` (`Options::bias_as_gradients`, found by nickorlabs in #30; `VICTORY_BIAS_AS_GRADIENTS=0` or `1` overrides). The bias cases in `image_addresses_of_16_bits_and_clamps` run under Wine again and pass. Pixel shaders keep the plain biased sample. - 24-bit depth. Apple GPUs have no 24-bit depth format, so D3DMetal keeps it as 32-bit float and rounds down when it stores 24 bits. The depth test clears to 0.75, where both roundings agree (PR #6). - Depth bounds. D3DMetal reports `DepthBoundsTestSupported` and does not apply the test (Metal has none): in the merged VIctory test a draw whose bounds exclude the stored depth still writes. Under Wine `Host::depth_bounds_supported` is false, so pipelines are made without bounds and one warning says so, which has the same effect and keeps the plain pipeline states. The test follows the flag.