From 9a8a9603cb4dd582ac53e8294f0de0abd6fd0cdd Mon Sep 17 00:00:00 2001 From: IxMxAMAR Date: Thu, 26 Nov 2026 05:44:47 +0530 Subject: [PATCH] Shaders: a buffer's size read from its descriptor is asked through a handle of its own, marked non-uniform (AMD's compiler crash) AMD's compiler (amdxc64.dll+0x861b9f; RX 9070 XT, driver 32.0.32029.2010) reads a null pointer compiling a buffer's getDimensions through the uniform heap handle the buffer's loads use, and the process ends. Five of the title's compute programs did so on main 32b31f7, each made alone with `victory translate --pipelines`: f16876929fa64f9f (bvh_skinning:CSComputeSkinningTreeAnimVelocity), 878933b499cfa14d (CSComputeSkinningTreeAnim), a567b9ef845fb680 (CSComputeTreeAnim), c1eee35dbc50a9ef (GpuParticleUpdate:CS_UpdateSkinnedAttachMeshTreeParticles) and 27ce027e7188bed3 (ped_standard_lod_muv_emissive:CS_RaytracingResolve). With every clean-up pass off (VICTORY_SHADER_OPT=0) the first and the fourth still did. descriptor_data now makes a heap handle of its own for a buffer's size, marked non-uniform (Shader::size_handle; not counted in nonuniform_handles, its index being the same in every lane); a descriptor loaded per lane keeps its handle, non-uniform already. All five compile. The idea, and the test's program, are those of pull request #452, rebased onto main. Test (dims_tests): the program's pipeline compiles on the hardware GPU and on WARP. On the RX 9070 XT without the change the test process ends inside amdxc64.dll. Co-authored-by: MalfoyDraco99 <171201207+MalfoyDraco99@users.noreply.github.com> --- crates/victory-shader/src/emit/dims_tests.rs | 121 +++++++++++++++++++ crates/victory-shader/src/emit/lower.rs | 13 +- crates/victory-shader/src/emit/mod.rs | 2 + crates/victory-shader/src/emit/shader.rs | 21 +++- 4 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 crates/victory-shader/src/emit/dims_tests.rs diff --git a/crates/victory-shader/src/emit/dims_tests.rs b/crates/victory-shader/src/emit/dims_tests.rs new file mode 100644 index 0000000000..70657ff610 --- /dev/null +++ b/crates/victory-shader/src/emit/dims_tests.rs @@ -0,0 +1,121 @@ +//! A buffer's size the program reads from its descriptor (a record count +//! taken as a number, `descriptor_data`) is asked through a heap handle of +//! its own, marked non-uniform: AMD's compiler (`amdxc64.dll`, RX 9070 XT, +//! driver 32.0.32029.2010) reads a null pointer compiling the query through +//! the uniform handle the buffer's loads use, and the process ends. The +//! program is the one of fork pull request #452 (MalfoyDraco99). + +use super::host::Host; +use super::{HostShader, lower, validator}; +use crate::native::{ + ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource, +}; +use crate::{io, ir, resource, structure}; + +/// A buffer descriptor loaded in a branch whose record count (dword 2) the +/// program takes as a number, the offset it loads at; the UAV of the +/// result loaded before the branch. +const WORDS: [u32; 19] = [ + 0xBE84_0300, // s_mov_b32 s4, s0 + 0xBE85_0380, // s_mov_b32 s5, 0 + 0xF408_0402, + 0xFA00_0020, // s_load_dwordx4 s[16:19], s[4:5], 0x20 (the result's UAV) + 0xBF8C_C07F, // s_waitcnt lgkmcnt(0) + 0x7D88_0081, // v_cmp_gt_u32 vcc_lo, 1, v0 (lane 0) + 0xBE94_3C6A, // s_and_saveexec_b32 s20, vcc_lo + 0xBF88_0008, // s_cbranch_execz to the end of the branch + 0xF408_0202, + 0xFA00_0000, // s_load_dwordx4 s[8:11], s[4:5], 0 (the buffer) + 0xBF8C_C07F, // s_waitcnt lgkmcnt(0) + 0x7E00_020A, // v_mov_b32 v0, s10: the record count as a number + 0xE030_1000, + 0x8002_0100, // buffer_load_dword v1, v0, s[8:11], 0 offen + 0xBF8C_3F70, // s_waitcnt vmcnt(0) + 0x887E_147E, // s_or_b32 exec_lo, exec_lo, s20 + 0xE070_1000, + 0x8004_0100, // buffer_store_dword v1, v0, s[16:19], 0 offen + 0xBF81_0000, // s_endpgm +]; + +/// [`WORDS`] lowered as a wave32 compute program with a table of an SRV and +/// a UAV at s0. +fn lowered() -> lower::Lowered { + let native = NativeProgram { + stage: HwStage::Cs, + wave64: false, + vgprs: 8, + layout: vec![RootParam { + root: RootType::Table, + table_offset_dwords: 0, + sgprs: 1, + base_register: 0, + user_data: 0, + slot: 0, + }], + used: [(ResourceType::Srv, 0), (ResourceType::Uav, 8)] + .into_iter() + .map(|(kind, table_offset_dwords)| UsedResource { + kind, + root: RootType::Table, + table_offset_dwords, + lower_bound: 0, + stride: 0, + dwords: 4, + user_data: 0, + slot: 0, + }) + .collect(), + io: StageIo::Cs(ComputeIo { + rsrc2: 0, + threads: [32, 1, 1], + }), + code: WORDS.iter().flat_map(|w| w.to_le_bytes()).collect(), + }; + let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build"); + let st = structure::structure(&p).expect("structure"); + let res = resource::track(&p, &native.layout, native.stage, Some(&st)).expect("track"); + let io = io::raise(&p, &native).expect("io::raise"); + lower::lower(&lower::Parts { + native: &native, + p: &p, + st: &st, + io: &io, + res: &res, + inputs: Vec::new(), + outputs: Vec::new(), + prims: Vec::new(), + mesh_stride: 0, + }) + .expect("lower") +} + +/// Without the handle of its own, AMD's compiler ends the test process here. +#[test] +fn a_buffer_size_read_as_a_number_compiles_on_the_host() { + let l = lowered(); + let shader = HostShader { + stage: HwStage::Cs, + dxil: l.dxil, + user_data: l.user_data, + samplers: l.samplers, + static_samplers: Vec::new(), + root_cbvs: l.root_cbvs, + root_srvs: l.root_srvs, + root_uavs: l.root_uavs, + root_indexed_views: Vec::new(), + }; + if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) { + v.validate(&mut shader.dxil.clone()) + .expect("DXIL validation failed"); + } + let _one = super::valu_tests::ONE_AT_A_TIME + .lock() + .unwrap_or_else(|e| e.into_inner()); + for warp in [false, true] { + let Ok(host) = Host::new(warp) else { + continue; + }; + host.compute_pipeline(&shader) + .unwrap_or_else(|e| panic!("warp={warp}: compute_pipeline failed: {e}")); + } +} diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 7a27510e6a..00102f889b 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -5877,7 +5877,18 @@ impl<'a> Lower<'a> { (first, props, image, uav, used) }; let src = Source::Loaded { load: site, first }; - let h = self.handle(site, &src, false, props)?; + // A buffer's size is asked through a handle of its own, marked non-uniform. AMD's + // compiler (`amdxc64.dll`, RX 9070 XT, driver 32.0.32029.2010) reads a null pointer + // compiling the query through a uniform heap handle, the one the buffer's loads use, + // and the process ends: five of the title's compute programs (tree skinning and + // animation, skinned particles, a ped material's ray resolve) did so. A descriptor + // loaded per lane keeps its own handle, non-uniform already. + let h = if image.is_none() && !self.x.res.waterfall.loads.contains(&site) { + let index = self.heap_index(site, first, false)?; + self.s.size_handle(self.cur, index, props) + } 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]); diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index 1439c5eece..3f39ee15d8 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -85,6 +85,8 @@ mod container; #[cfg(all(test, windows))] mod diff_tests; #[cfg(all(test, windows))] +mod dims_tests; +#[cfg(all(test, windows))] mod intersect_tests; #[cfg(all(windows, any(test, feature = "host")))] pub mod host; diff --git a/crates/victory-shader/src/emit/shader.rs b/crates/victory-shader/src/emit/shader.rs index 28a36f1bed..c477e6e63b 100644 --- a/crates/victory-shader/src/emit/shader.rs +++ b/crates/victory-shader/src/emit/shader.rs @@ -401,6 +401,26 @@ impl Shader { sampler: bool, nonuniform: bool, props: (u32, u32), + ) -> V { + self.nonuniform_handles += u32::from(nonuniform); + self.marked_heap_handle(block, index, sampler, nonuniform, props) + } + + /// The handle a buffer's size is asked through (`descriptor_data`): + /// marked non-uniform for AMD's compiler, which reads a null pointer + /// compiling the query through a uniform one, and not counted in + /// `nonuniform_handles`, its index being the same in every lane. + pub fn size_handle(&mut self, block: usize, index: V, props: (u32, u32)) -> V { + self.marked_heap_handle(block, index, false, true, props) + } + + fn marked_heap_handle( + &mut self, + block: usize, + index: V, + sampler: bool, + nonuniform: bool, + props: (u32, u32), ) -> V { // a view index past the host heap (`set_view_heap_limit`) reads its null slot instead let limit = VIEW_LIMIT.load(std::sync::atomic::Ordering::Relaxed); @@ -430,7 +450,6 @@ impl Shader { } else { flags::RESOURCE_HEAP }; - self.nonuniform_handles += u32::from(nonuniform); let (s, n) = (self.m.bool(sampler), self.m.bool(nonuniform)); let h = self.op( block,