Skip to content
Draft
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
2 changes: 2 additions & 0 deletions d3d12/crates/leonida-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,8 @@ fn create() -> windows::core::Result<Host> {
{
tracing::info!(min = o1.WaveLaneCountMin, max = o1.WaveLaneCountMax, "host wave lanes");
leonida_shader::emit::set_host_max_wave(o1.WaveLaneCountMax);
// and the narrowest: below 32 (Intel Arc) mesh programs ask for waves of 32
leonida_shader::emit::set_host_min_wave(o1.WaveLaneCountMin);
// (RDNA has waves of 32 and 64, the vendor's older cards of 64 alone.)
let amd = crate::indirect::amd(&device);
leonida_shader::emit::set_host_rdna(amd && o1.WaveLaneCountMin == 32 && o1.WaveLaneCountMax == 64);
Expand Down
2 changes: 1 addition & 1 deletion d3d12/crates/leonida-gpu/src/shader_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn paired_override(container: &[u8], mut shader: HostShader) -> HostShader {
let Some(dir) = DIR.get_or_init(|| std::env::var_os("LEONIDA_PAIRED_SHADER_DIR").map(PathBuf::from)) else {
return shader;
};
if shader.stage != HwStage::Cs || leonida_shader::emit::host_settings() != [32, 999_999, 0, 0] {
if shader.stage != HwStage::Cs || leonida_shader::emit::host_settings() != [32, 999_999, 0, 0, 0] {
return shader;
}
let digest = blake3::hash(container).to_hex();
Expand Down
84 changes: 80 additions & 4 deletions d3d12/crates/leonida-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,68 @@ pub fn set_host_max_wave(lanes: u32) {
HOST_MAX_WAVE.store(lanes, std::sync::atomic::Ordering::Relaxed);
}

/// The narrowest wave the host GPU runs (0: not known); see [`set_host_min_wave`].
static HOST_MIN_WAVE: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);

/// The host GPU's `WaveLaneCountMin`: below 32 (Intel Arc runs 16 to 32) the driver may run a
/// program in waves narrower than the 32 lanes our translation of the console's code assumes,
/// unless the program asks for its wave size ([`mesh_wave32`]).
pub fn set_host_min_wave(lanes: u32) {
HOST_MIN_WAVE.store(lanes, std::sync::atomic::Ordering::Relaxed);
}

/// Whether mesh programs ask for waves of 32 (`WaveSize(32)`), as compute programs that need
/// the console's lanes already do: on by default on a host whose waves can be narrower than 32
/// lanes but reach 32 (Intel Arc: 16 to 32), where the driver otherwise picks the width.
/// `LEONIDA_MESH_WAVE32=1` asks it on any host that runs waves of 32 (to try it on NVIDIA,
/// whose waves are always 32), `=0` never.
///
/// A mesh program has no cross-lane operations here (`Lower::cross_lane` refuses them), but the
/// console's code still counts on its group being whole waves of 32 or 64: LDS shared within a
/// wave without barriers, branches the wave takes together. In waves of 16 two host waves run
/// what was one console wave, unordered. On a B580 (driver 32.0.101.8991) every crash was a
/// device hang inside mesh programs, also with each mesh draw cut to one pixel, and none with
/// mesh draws skipped (2026-11-30/12-01).
pub(super) fn mesh_wave32() -> bool {
let set = std::env::var("LEONIDA_MESH_WAVE32").ok();
mesh_wave32_for(set.as_deref(), host_min_wave(), host_max_wave())
}

/// [`mesh_wave32`] for a value of `LEONIDA_MESH_WAVE32` and the host's narrowest and widest wave.
fn mesh_wave32_for(set: Option<&str>, min: u32, max: u32) -> bool {
// (0: the host's waves are not known yet, so nothing is asked of it)
let runs_32 = min > 0 && (min..=max).contains(&32);
match set {
Some("0") => false,
Some("1") => runs_32,
_ => runs_32 && min < 32,
}
}

#[cfg(test)]
mod mesh_wave32_tests {
use super::mesh_wave32_for;

#[test]
fn mesh_programs_ask_for_waves_of_32_only_where_the_driver_could_pick_narrower() {
// Intel Arc (16 to 32): on by default, off when asked.
assert!(mesh_wave32_for(None, 16, 32));
assert!(!mesh_wave32_for(Some("0"), 16, 32));
// NVIDIA (always 32) and AMD RDNA (32 and 64): nothing to fix, unless asked on a host
// that runs waves of 32.
assert!(!mesh_wave32_for(None, 32, 32));
assert!(!mesh_wave32_for(None, 32, 64));
assert!(mesh_wave32_for(Some("1"), 32, 32));
assert!(mesh_wave32_for(Some("1"), 32, 64));
// A host that never runs waves of 32 (older AMD: 64 alone) would refuse `WaveSize(32)`.
assert!(!mesh_wave32_for(Some("1"), 64, 64));
assert!(!mesh_wave32_for(None, 64, 64));
// Not known yet (0): nothing asked.
assert!(!mesh_wave32_for(None, 0, 0));
assert!(!mesh_wave32_for(None, 0, 32));
}
}

thread_local! {
/// See [`take_needs`].
static NEEDS: std::cell::RefCell<Vec<usize>> = const { std::cell::RefCell::new(Vec::new()) };
Expand Down Expand Up @@ -1824,10 +1886,17 @@ fn own_bits_on() -> bool {
}

/// What the translator was told of the host, for a cache's key: the widest
/// wave, the view heap's limit, where the raw twins of its views start, and
/// what it writes for the console's own hardware.
pub fn host_settings() -> [u32; 4] {
[host_max_wave(), super::shader::view_heap_limit(), super::shader::raw_views(), host_rdna()]
/// wave, the view heap's limit, where the raw twins of its views start, what
/// it writes for the console's own hardware, and whether mesh programs ask
/// for waves of 32 ([`mesh_wave32`], which the narrowest wave decides).
pub fn host_settings() -> [u32; 5] {
[
host_max_wave(),
super::shader::view_heap_limit(),
super::shader::raw_views(),
host_rdna(),
u32::from(mesh_wave32()),
]
}

static HOST_RDNA: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
Expand Down Expand Up @@ -2183,6 +2252,10 @@ pub(super) fn host_max_wave() -> u32 {
HOST_MAX_WAVE.load(std::sync::atomic::Ordering::Relaxed)
}

pub(super) fn host_min_wave() -> u32 {
HOST_MIN_WAVE.load(std::sync::atomic::Ordering::Relaxed)
}

#[cfg(test)]
thread_local! {
/// A test's host wave size, for its thread only ([`set_host_max_wave`] is the process's).
Expand Down Expand Up @@ -2791,6 +2864,9 @@ impl<'a> Lower<'a> {
max_primitives: ms.max_primitives,
topology: ms.topology,
});
if mesh_wave32() {
s.wave_size = Some(32);
}
// Barriers order the lanes' LDS accesses; the console's
// allocation request needs them too, but the host does not. One
// lane sends that request, and what it dominates (the path for
Expand Down
2 changes: 1 addition & 1 deletion d3d12/crates/leonida-shader/src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ use crate::{ShaderError, io, ir, resource, structure};

pub use bitcode::census;
pub use sampler::StaticSampler;
pub use lower::{PixelMask, host_settings, set_host_max_wave, set_host_nvidia, set_host_rdna, take_pack_why, with_pixel_mask};
pub use lower::{PixelMask, host_settings, set_host_max_wave, set_host_min_wave, set_host_nvidia, set_host_rdna, take_pack_why, with_pixel_mask};

/// The row of its output signature a translated vertex or mesh program (`dxil`, as [`translate`]
/// makes it) writes `SV_Position` in: where a pixel program translated for the console's sample
Expand Down
3 changes: 2 additions & 1 deletion d3d12/docs/switches.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It says which names exist and what the code's own comments say of them. It is **
settings and says nothing of what a switch gains: for that read [findings.md](findings.md) first,
and [how to measure frame rates](../../docs/measuring_fps.md) before trying one.

673 names; 633 have a sentence of a comment below, the others no comment or only a bare list.
674 names; 634 have a sentence of a comment below, the others no comment or only a bare list.

- **Kind**: how the code reads it, where that can be told. "see the code" is a number, a path, a list or a
switch read some other way; its default is not given here.
Expand Down Expand Up @@ -712,6 +712,7 @@ and [how to measure frame rates](../../docs/measuring_fps.md) before trying one.
| `LEONIDA_MED3_CLAMP` | see the code | | | yes | `leonida-shader/emit/lower.rs` | Whether the median of a value and two constants that are normal numbers or infinities is a clamp (`Lower::med3_clamp`; `LEONIDA_MED3_CLAMP=0`: the general form, as before): the same bits, with fewer instructions (bounds... |
| `LEONIDA_MESH_COUNTS` | see the code | | | yes | `leonida-shader/emit/lower.rs` +2 | A mesh program's own output counts (`LEONIDA_MESH_COUNTS=1`; off by default). |
| `LEONIDA_MESH_LOOP_EXITS` | see the code | | | yes | `leonida-shader/emit/lower.rs` | `LEONIDA_MESH_LOOP_EXITS`, on unless `0` (then a lane at the limit leaves the program from inside the loop, [`Lower::abort_block`], as before). |
| `LEONIDA_MESH_WAVE32` | see the code | | | | `leonida-shader/emit/lower.rs` | `LEONIDA_MESH_WAVE32=1` asks it on any host that runs waves of 32 (to try it on NVIDIA, whose waves are always 32), `=0` never. |
| `LEONIDA_MINMAX_NONZERO` | see the code | | | yes | `leonida-shader/emit/lower.rs` | Whether a float maximum or minimum with a constant whose magnitude bits are not all zero leaves out the test for two zeros (`Lower::max_min`; `LEONIDA_MINMAX_NONZERO=0`: as before). |
| `LEONIDA_NAN_EXACT` | see the code | | | yes | `leonida-shader/emit/lower.rs` +1 | Whether a float compare and the program's other NaN tests tell a NaN by its bits (`\|x\| &gt; the bits of infinity`, [`Lower::is_nan_f32`]) instead of an unordered `fcmp` (on; `LEONIDA_NAN_EXACT=0`: as before). |
| `LEONIDA_NAN_SCRUB_INF` | see the code | | | | `leonida-shader/emit/lower.rs` +1 | Whether the scrub (`LEONIDA_NAN_SCRUB`) holds an infinity at the largest finite half-float of its sign, 65504, instead of making it 0 (on; `LEONIDA_NAN_SCRUB_INF=0`: 0, as before). |
Expand Down
Loading