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
39 changes: 39 additions & 0 deletions d3d12/crates/leonida-shader/src/emit/amd_size.rs
Original file line number Diff line number Diff line change
@@ -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<V, Why> {
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)
}
}
10 changes: 9 additions & 1 deletion d3d12/crates/leonida-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -76,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,
Expand Down Expand Up @@ -3780,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,
Expand Down Expand Up @@ -8778,7 +8786,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]);
Expand Down
10 changes: 10 additions & 0 deletions d3d12/crates/leonida-shader/src/emit/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 29 additions & 3 deletions d3d12/crates/leonida-shader/src/emit/size_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HostShader, ShaderError> {
fn lower_cs(words: &[u32], srv: (u8, u32)) -> Result<lower::Lowered, ShaderError> {
let param = |root, user_data| RootParam {
root,
table_offset_dwords: 0,
Expand Down Expand Up @@ -52,7 +52,7 @@ fn build(words: &[u32], srv: (u8, u32)) -> Result<HostShader, ShaderError> {
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,
Expand All @@ -62,7 +62,12 @@ fn build(words: &[u32], srv: (u8, u32)) -> Result<HostShader, ShaderError> {
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<HostShader, ShaderError> {
let lowered = lower_cs(words, srv)?;
let shader = HostShader {
stage: HwStage::Cs,
dxil: lowered.dxil,
Expand Down Expand Up @@ -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]
Expand Down