Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions crates/victory-gpu/src/shader_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> = 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.
Expand Down Expand Up @@ -508,6 +517,17 @@ fn decode(bytes: &[u8]) -> Option<Result<HostShader, String>> {
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,
Expand Down
2 changes: 1 addition & 1 deletion docs/macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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, 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.
Expand Down