From 6a2b449920d0cc1ffc83b5932fa61f97b7feb956 Mon Sep 17 00:00:00 2001 From: Kartik Vats <40257338+prosperkartik@users.noreply.github.com> Date: Mon, 30 Nov 2026 23:50:48 +0530 Subject: [PATCH 1/2] D3D12-AMD-RDNA4-SIZE-HANDLE: give a buffer's size query its own non-uniform handle On AMD RDNA 4 (RX 9070 GRE, driver 32.0.32015.7029, Windows 11) the driver's shader compiler crashes the title (0xc0000005 in amdxc64.dll+0x85fd0f) while building compute pipelines at the first story cutscene. These programs ask a buffer's size through the same uniform heap handle the buffer's loads use. In descriptor_data the size query now gets a heap handle of its own, marked non-uniform and not counted in nonuniform_handles (emit/amd_size.rs). Waterfall loads and images keep the shared handle as before. Port of trustno1-coder/VIctory-NVIDIA #559 (66da8aaa), the size-query part of DexNom/VIctory PR 62 (MrToucann); the approach is IxMxAMAR's (fxceless/victory-fork#1213). Mandy's twin_handle stands in for VIctory's raw_twin + heap_handle_at pair; otherwise unchanged. --- .../leonida-shader/src/emit/amd_size.rs | 39 +++++++++++++++++++ d3d12/crates/leonida-shader/src/emit/lower.rs | 5 ++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 d3d12/crates/leonida-shader/src/emit/amd_size.rs diff --git a/d3d12/crates/leonida-shader/src/emit/amd_size.rs b/d3d12/crates/leonida-shader/src/emit/amd_size.rs new file mode 100644 index 0000000000..cfbb43c07b --- /dev/null +++ b/d3d12/crates/leonida-shader/src/emit/amd_size.rs @@ -0,0 +1,39 @@ +//! Port of trustno1-coder/VIctory-NVIDIA #559 (commit 66da8aaa), itself DexNom/VIctory PR 62 +//! (MrToucann, 2152e55b), size-query part only: AMD's shader compiler (amdxc64, RX 9060 XT / +//! 9070 XT / 9070 GRE) reads a null pointer building a pipeline where a buffer's size is asked +//! through the same uniform heap handle the buffer's loads use (fxceless/victory-fork#1213, +//! IxMxAMAR). Here the size gets a heap handle of its own, marked non-uniform, so the compiler +//! cannot merge the two. Seen here (RX 9070 GRE) as `amdxc64.dll+0x85fd0f` at the first story +//! cutscene, 2026-11-30. +//! +//! The shader cache keys on the translator's sources (`leonida-gpu`'s `build.rs`), so +//! translations cached before this change are not served after it. + +use super::super::bitcode::V; +use super::{Lower, Site, Source, Why}; + +impl Lower<'_> { + /// The handle a buffer's size (`descriptor_data`) is asked through: for a buffer whose + /// descriptor every lane shares, one of its own marked non-uniform, not counted in + /// `nonuniform_handles` (its index is the same in every lane); otherwise the view's + /// handle as before. The index is the loads' own, raw twin included (`raw_twin`), so + /// the size is the view's the loads read. + pub(super) fn size_view_handle( + &mut self, + site: Site, + first: u8, + src: &Source, + props: (u32, u32), + buffer: bool, + ) -> Result { + if !buffer || self.x.res.waterfall.loads.contains(&site) { + return self.handle(site, src, false, props); + } + let index = self.heap_index(site, first, false)?; + // Mandy's `twin_handle` is VIctory's `raw_twin` + `heap_handle_at` (bounded by the + // twin) + the twin's bias noted: the same handle, here asked for as non-uniform. + let h = self.twin_handle(src, index, false, true, props); + self.s.nonuniform_handles -= 1; + Ok(h) + } +} diff --git a/d3d12/crates/leonida-shader/src/emit/lower.rs b/d3d12/crates/leonida-shader/src/emit/lower.rs index 773af41197..1c1e24a27d 100644 --- a/d3d12/crates/leonida-shader/src/emit/lower.rs +++ b/d3d12/crates/leonida-shader/src/emit/lower.rs @@ -25,6 +25,9 @@ use crate::structure::{Atom, Bool, LaneExit, Structured}; /// it shares the emitter's state. #[path = "intersect.rs"] pub(super) mod intersect; +/// VIctory #559 / DexNom PR 62: a buffer size query gets its own handle (AMD RDNA 4 compiler crash). +#[path = "amd_size.rs"] +mod amd_size; /// Format loads that move 16-bit halves without the d16 form, a child /// module so it shares the emitter's state. @@ -8778,7 +8781,7 @@ impl<'a> Lower<'a> { (first, props, image, uav, used) }; let src = Source::Loaded { load: site, first }; - let h = self.handle(site, &src, false, props)?; + let h = self.size_view_handle(site, first, &src, props, image.is_none())?; // amd_size.rs (VIctory #559) let c = self.cur; let i32t = self.s.t.i32; let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]); From bcb6087c68f922e7cd15d7c1c1d74fa67101e697 Mon Sep 17 00:00:00 2001 From: Kartik Vats <40257338+prosperkartik@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:36:47 +0530 Subject: [PATCH 2/2] D3D12-AMD-RDNA4-SIZE-HANDLE: test that a buffer's size gets a handle of its own Adds a_buffers_size_is_asked_through_a_handle_of_its_own (emit/size_tests.rs) and a test-only count of the heap handles marked non-uniform (nonuniform_marked): the size handle is marked non-uniform but on purpose not counted in nonuniform_handles, so that count alone cannot tell the change. The program of the_record_count_of_a_structured_buffer_read_by_index asks two buffers' sizes (descriptor_data: the structured buffer's at s[8:11], the raw UAV's at s[20:23]). With the change both get a handle of their own: marked 2, counted 0. With the change's call in descriptor_data reverted the test fails (marked 0). It only lowers the program, so it runs on any host and needs no RDNA 4 card. size_tests' build is split into lower_cs (the lowering) and build (the host shader) for it. --- d3d12/crates/leonida-shader/src/emit/lower.rs | 5 +++ .../crates/leonida-shader/src/emit/shader.rs | 10 ++++++ .../leonida-shader/src/emit/size_tests.rs | 32 +++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/d3d12/crates/leonida-shader/src/emit/lower.rs b/d3d12/crates/leonida-shader/src/emit/lower.rs index 1c1e24a27d..aac85005e3 100644 --- a/d3d12/crates/leonida-shader/src/emit/lower.rs +++ b/d3d12/crates/leonida-shader/src/emit/lower.rs @@ -79,6 +79,9 @@ pub(crate) struct Lowered { /// Heap handles made with a non-uniform index. #[cfg(test)] pub nonuniform_handles: u32, + /// Heap handles marked non-uniform, counted in `nonuniform_handles` or not. + #[cfg(test)] + pub nonuniform_marked: u32, /// Whether the program uses wave operations. #[cfg(test)] pub wave_ops: bool, @@ -3783,6 +3786,8 @@ impl<'a> Lower<'a> { #[cfg(test)] nonuniform_handles: self.s.nonuniform_handles, #[cfg(test)] + nonuniform_marked: self.s.nonuniform_marked, + #[cfg(test)] wave_ops: self.s.flags & flags::WAVE_OPS != 0, #[cfg(test)] early_depth: self.s.flags & flags::EARLY_DEPTH_STENCIL != 0, diff --git a/d3d12/crates/leonida-shader/src/emit/shader.rs b/d3d12/crates/leonida-shader/src/emit/shader.rs index f1ed73fc47..bbd043f4da 100644 --- a/d3d12/crates/leonida-shader/src/emit/shader.rs +++ b/d3d12/crates/leonida-shader/src/emit/shader.rs @@ -362,6 +362,10 @@ pub(crate) struct Shader { pub flags: u64, /// Heap handles made with a non-uniform index. pub nonuniform_handles: u32, + /// Heap handles marked non-uniform, whether or not counted in `nonuniform_handles` + /// (a buffer's size handle, `amd_size.rs`, is marked but not counted). + #[cfg(test)] + pub nonuniform_marked: u32, /// A compute shader with this thread-group size. pub threads: Option<[u32; 3]>, /// The wave size a compute shader requires (`WaveSize`). @@ -422,6 +426,8 @@ impl Shader { root_uavs: Vec::new(), flags: 0, nonuniform_handles: 0, + #[cfg(test)] + nonuniform_marked: 0, threads: None, wave_size: None, lds_dwords: 0, @@ -661,6 +667,10 @@ impl Shader { flags::RESOURCE_HEAP }; self.nonuniform_handles += u32::from(nonuniform); + #[cfg(test)] + { + self.nonuniform_marked += u32::from(nonuniform); + } let (s, n) = (self.m.bool(sampler), self.m.bool(nonuniform)); let h = self.op( block, diff --git a/d3d12/crates/leonida-shader/src/emit/size_tests.rs b/d3d12/crates/leonida-shader/src/emit/size_tests.rs index ae65f1dfeb..4df25efdcb 100644 --- a/d3d12/crates/leonida-shader/src/emit/size_tests.rs +++ b/d3d12/crates/leonida-shader/src/emit/size_tests.rs @@ -14,7 +14,7 @@ use crate::{ShaderError, io, ir, resource, structure}; /// Lowers a wave32 compute program of one thread with the table of views at /// s0 and the view heap at s1; `srv`: the dwords and the stride of the used /// resource at the table's first entry (the third is a raw UAV). -fn build(words: &[u32], srv: (u8, u32)) -> Result { +fn lower_cs(words: &[u32], srv: (u8, u32)) -> Result { let param = |root, user_data| RootParam { root, table_offset_dwords: 0, @@ -52,7 +52,7 @@ fn build(words: &[u32], srv: (u8, u32)) -> Result { let st = structure::structure(&p)?; let res = resource::track(&p, &native.layout, native.stage, Some(&st))?; let io = io::raise(&p, &native)?; - let lowered = lower::lower(&lower::Parts { + lower::lower(&lower::Parts { native: &native, p: &p, st: &st, @@ -62,7 +62,12 @@ fn build(words: &[u32], srv: (u8, u32)) -> Result { outputs: Vec::new(), prims: Vec::new(), mesh_stride: 0, - })?; + }) +} + +/// [`lower_cs`], as the host shader the tests run (validated by `dxil.dll` when present). +fn build(words: &[u32], srv: (u8, u32)) -> Result { + let lowered = lower_cs(words, srv)?; let shader = HostShader { stage: HwStage::Cs, dxil: lowered.dxil, @@ -252,6 +257,27 @@ fn the_record_count_of_a_structured_buffer_read_by_index() { } } +/// AMD's shader compiler (RDNA 4, `amdxc64.dll`) crashes building a pipeline whose program +/// asks a buffer's size through the uniform heap handle its loads use. The program of +/// [`the_record_count_of_a_structured_buffer_read_by_index`] asks two buffers' sizes +/// (`descriptor_data`): the structured buffer's at its load in s[8:11] and the raw UAV's at the +/// table's load in s[20:23]. Each size now gets a heap handle of its own (`amd_size.rs`), +/// marked non-uniform and not counted among the non-uniform handles, for its index is the +/// same in every lane: before, both went through the loads' uniform handles and none was. +#[test] +fn a_buffers_size_is_asked_through_a_handle_of_its_own() { + let load = [ + 0x7E06_0280, // v_mov_b32 v3, 0 + 0xE030_2000, // buffer_load_dword v5, v3, s[8:11], 0 idxen + 0x8002_0503, + 0x7E0A_0505, // v_readfirstlane_b32 s5, v5 + ]; + let words = [TABLE, COUNT, &load, STORE].concat(); + let l = lower_cs(&words, (4, 16)).expect("translates"); + assert_eq!(l.nonuniform_handles, 0); + assert_eq!(l.nonuniform_marked, 2); +} + /// An upscaler counts its groups in a texture with atomics and sets the /// counter back with a store of one channel, 16 bits wide. #[test]