Skip to content
Open
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
7 changes: 7 additions & 0 deletions d3d12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ Hookshot's release builds do; `-NoPrecompile` skips it.
down for it (`LEONIDA_PIPELINE_BOOK=1`, set by the launcher), so the first session in a place still makes that
place's pipelines as it meets them; from the next start on they are made ahead.

On a Radeon RX 9070 XT with driver 32.0.32029.2010, some compute pipelines fault in
`amdxc64.dll` while the driver compiles DXIL. Starting Leonida with
`LEONIDA_SEPARATE_SIZE_HANDLES=1` gives buffer size queries their own heap handles.
The shader cache distinguishes this setting and rebuilds translations on the next run.
A prior warm run can leave failed keys in `pipelines/warm-bad.txt` under the cache directory.
With Leonida closed, move that file aside to retry those pipelines with the new setting.

---

> **Before you play**
Expand Down
10 changes: 6 additions & 4 deletions d3d12/crates/leonida-shader/src/emit/bitcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod opt;
pub(crate) struct Ty(u32);

/// What the lowering made an instruction for, where it says so (counted by
/// [`census`], nothing else reads it).
/// [`census`] and used by the optimiser).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Tag {
/// The select that keeps a register's old value in lanes a guard masks.
Expand All @@ -23,6 +23,8 @@ pub(crate) enum Tag {
Clamp,
/// The iteration counter and limit test the translation adds to a loop.
Counter,
/// A buffer size query's heap handle must stay distinct from memory accesses.
SizeHandle,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -390,11 +392,11 @@ impl Module {
g
}

/// Notes what instruction `v` is for (see [`Tag`]); only when modules
/// are counted.
/// Notes what instruction `v` is for (see [`Tag`]).
/// Keep optimisation tags even when instruction counts are disabled.
pub fn tag(&mut self, v: V, tag: Tag) {
if let V::Inst(i) = v
&& census::enabled()
&& (tag == Tag::SizeHandle || census::enabled())
{
self.tags.insert(i, tag);
}
Expand Down
2 changes: 1 addition & 1 deletion d3d12/crates/leonida-shader/src/emit/bitcode/census.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Module {
Some(Tag::Guard) => c.counts[k("guard_select")] += 1,
Some(Tag::Clamp) => c.counts[k("clamp")] += 1,
Some(Tag::Counter) => c.counts[k("loop_counter")] += 1,
None => {}
Some(Tag::SizeHandle) | None => {}
}
let _ = V::Inst(i);
}
Expand Down
20 changes: 20 additions & 0 deletions d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ impl Module {
let class = self.class(&effects, i);
if passes & REUSE != 0
&& class == Effect::Pure
&& self.tags.get(&i) != Some(&super::Tag::SizeHandle)
&& let Some(key) = self.key(i)
{
if let Some(&first) = known.get(&key) {
Expand Down Expand Up @@ -1915,6 +1916,25 @@ mod tests {
}
}

#[test]
fn buffer_size_handles_are_kept_separate_when_reusing_values() {
let Kit { mut m, b, pure, writes, .. } = kit();
let (index, sampler) = (m.i32(4), m.i32(0));
let size = m.call(b, pure, vec![index, sampler]);
m.tag(size, super::super::Tag::SizeHandle);
let access = m.call(b, pure, vec![index, sampler]);
let duplicate = m.call(b, pure, vec![index, sampler]);
let unread_query = m.call(b, pure, vec![index, sampler]);
m.tag(unread_query, super::super::Tag::SizeHandle);
m.call(b, writes, vec![size, duplicate]);
m.terminate(b, Term::Ret);

m.optimise(REUSE | DEAD);

assert_eq!(stores(&m, b), [[size, access]]);
assert_eq!(body(&m, b), 3);
}

#[test]
fn a_value_is_reused_below_its_block_and_not_beside_it() {
let Diamond {
Expand Down
34 changes: 30 additions & 4 deletions d3d12/crates/leonida-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub(crate) struct Lowered {
/// Heap handles made with a non-uniform index.
#[cfg(test)]
pub nonuniform_handles: u32,
/// Dedicated size-query handles counted before dead-code removal.
#[cfg(test)]
pub size_query_handles: u32,
/// Whether the program uses wave operations.
#[cfg(test)]
pub wave_ops: bool,
Expand Down Expand Up @@ -1655,6 +1658,14 @@ pub fn host_settings() -> [u32; 4] {
[host_max_wave(), super::shader::view_heap_limit(), super::shader::raw_views(), host_rdna()]
}

/// Whether buffer size queries use dedicated, conservatively non-uniform handles.
pub(super) fn separate_size_handles() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| {
std::env::var_os("LEONIDA_SEPARATE_SIZE_HANDLES").is_some_and(|v| v == "1")
})
}

static HOST_RDNA: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Tells the translator that the host's shaders run on AMD RDNA hardware
Expand Down Expand Up @@ -3489,6 +3500,8 @@ impl<'a> Lower<'a> {
#[cfg(test)]
nonuniform_handles: self.s.nonuniform_handles,
#[cfg(test)]
size_query_handles: self.s.size_query_handles,
#[cfg(test)]
wave_ops: self.s.flags & flags::WAVE_OPS != 0,
#[cfg(test)]
early_depth: self.s.flags & flags::EARLY_DEPTH_STENCIL != 0,
Expand Down Expand Up @@ -8278,7 +8291,19 @@ impl<'a> Lower<'a> {
(first, props, image, uav, used)
};
let src = Source::Loaded { load: site, first };
let h = self.handle(site, &src, false, props)?;
// On the affected AMD driver, sharing a buffer handle between
// getDimensions and a memory access can crash the DXIL compiler.
let h = if image.is_none() && separate_size_handles() {
let index = self.heap_index(site, first, false)?;
let (index, bias) = self.raw_twin(&src, index, false, props);
let h = self.s.heap_handle_at(self.cur, (index, bias.is_some()), false, true, props, true);
if let Some(bias) = bias {
self.twin_bias.insert(h, bias);
}
h
} else {
self.handle(site, &src, false, props)?
};
let c = self.cur;
let i32t = self.s.t.i32;
let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]);
Expand Down Expand Up @@ -9193,7 +9218,7 @@ impl<'a> Lower<'a> {
/// for [`Lower::buffer_op`].
fn twin_handle(&mut self, src: &Source, index: V, sampler: bool, nonuniform: bool, props: (u32, u32)) -> V {
let (index, bias) = self.raw_twin(src, index, sampler, props);
let h = self.s.heap_handle_at(self.cur, index, sampler, nonuniform, props, bias.is_some());
let h = self.s.heap_handle_at(self.cur, (index, bias.is_some()), sampler, nonuniform, props, false);
if let Some(bias) = bias {
self.twin_bias.insert(h, bias);
}
Expand Down Expand Up @@ -15595,8 +15620,9 @@ mod tests {
for words in [WATERFALL_JOINED_WAVE32, WATERFALL_JOINED_MIXED_WAVE32] {
let (layout, used) = texture_and_uav_tables();
let l = lower_wave(words, layout, used, false).unwrap();
// One image handle, from the join's per-lane heap index.
assert_eq!(l.nonuniform_handles, 1);
// One per-lane image handle, plus the optional size-query handle.
// This counter is collected before unused queries are removed.
assert_eq!(l.nonuniform_handles - l.size_query_handles, 1);
assert!(!l.wave_ops);
}
}
Expand Down
22 changes: 17 additions & 5 deletions d3d12/crates/leonida-shader/src/emit/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ pub(crate) struct Shader {
pub flags: u64,
/// Heap handles made with a non-uniform index.
pub nonuniform_handles: u32,
/// Dedicated size-query handles counted before dead-code removal.
#[cfg(test)]
pub size_query_handles: u32,
/// A compute shader with this thread-group size.
pub threads: Option<[u32; 3]>,
/// The wave size a compute shader requires (`WaveSize`).
Expand Down Expand Up @@ -422,6 +425,8 @@ impl Shader {
root_uavs: Vec::new(),
flags: 0,
nonuniform_handles: 0,
#[cfg(test)]
size_query_handles: 0,
threads: None,
wave_size: None,
lds_dwords: 0,
Expand Down Expand Up @@ -617,19 +622,19 @@ impl Shader {
nonuniform: bool,
props: (u32, u32),
) -> V {
self.heap_handle_at(block, index, sampler, nonuniform, props, false)
self.heap_handle_at(block, (index, false), sampler, nonuniform, props, false)
}

/// [`Shader::heap_handle`]; `bounded`: the index is inside the host's heap already
/// ([`raw_views`]).
/// [`Shader::heap_handle`]; `bounded` means the index is already inside the host's heap
/// ([`raw_views`]). `separate` protects a buffer size query from handle reuse.
pub fn heap_handle_at(
&mut self,
block: usize,
index: V,
(index, bounded): (V, bool),
sampler: bool,
nonuniform: bool,
props: (u32, u32),
bounded: bool,
separate: bool,
) -> V {
// a view index past the host heap (`set_view_heap_limit`) reads its null slot instead
// (the raw twins of buffer views lie past it: such an index comes bounded)
Expand Down Expand Up @@ -661,6 +666,10 @@ impl Shader {
flags::RESOURCE_HEAP
};
self.nonuniform_handles += u32::from(nonuniform);
#[cfg(test)]
{
self.size_query_handles += u32::from(separate);
}
let (s, n) = (self.m.bool(sampler), self.m.bool(nonuniform));
let h = self.op(
block,
Expand All @@ -670,6 +679,9 @@ impl Shader {
218,
&[index, s, n],
);
if separate {
self.m.tag(h, super::bitcode::Tag::SizeHandle);
}
self.annotate(block, h, props.0, props.1)
}

Expand Down
5 changes: 3 additions & 2 deletions d3d12/crates/leonida-shader/src/emit/waterfall_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ fn build(words: &[u32], buffers: bool) -> HostShader {
mesh_stride: 0,
})
.expect("lower");
// One access with each lane's own heap index, no wave operation.
assert_eq!(lowered.nonuniform_handles, 1);
// One per-lane access, plus the optional conservative size-query handle.
// The counter includes queries that are removed later as unused.
assert_eq!(lowered.nonuniform_handles - lowered.size_query_handles, 1);
assert!(!lowered.wave_ops);
let shader = HostShader {
stage: HwStage::Cs,
Expand Down
6 changes: 6 additions & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ passing `spirv-val --target-env vulkan1.2`: `ps_ops` 76→33 instructions and 7,
60→22 and 4,560→1,676, `ps_sdwa` 61→19 and 5,720→2,192, `ps_vcc_x2` 63→26 and 5,260→2,552, and
`ps_fnan_med3` 76→26 and 8,060→4,264.

### Native D3D12 translator contract

| Piece | Contract |
|---|---|
| AMD buffer size queries | With `LEONIDA_SEPARATE_SIZE_HANDLES=1`, a buffer's `getDimensions` gets a separate heap handle to the same view. The reuse pass keeps the handle separate from loads and stores, while dead queries can still be removed. Descriptor binding, raw-twin bias and record-count arithmetic retain their existing rules. Confirmed by the synthetic `buffer_size_handles_are_kept_separate_when_reusing_values` regression and offline native D3D12 pipeline compilation on Radeon RX 9070 XT (driver 32.0.32029.2010). See [VIctory #2122](https://github.com/Marvisak/VIctory/pull/2122) for the related finding. |

### GPU tests (RTX 4060 Ti)

`runtime\test\vk_harness.cpp` (CMake target `vk_harness`, built by `scripts\build_runtime.ps1` or
Expand Down