From 79f077793044893404ae74f449e7f05ee530f988 Mon Sep 17 00:00:00 2001 From: Toucann Date: Wed, 25 Nov 2026 18:44:32 -0700 Subject: [PATCH 1/2] Keep AMD's shader compiler from failing on 13 of the title's programs amdxc64 8.18.10.0514 (Radeon RX 9060 XT, driver 32.0.32029.2010) dereferences null, or ends the process, creating the pipelines of 13 programs as translated. They were found by creating every program's pipeline (2,684 compute; 23,279 others with a stand-in partner stage) in a process of its own. - Four compute programs load from a raw view at an offset computed from that view's own size, with a branch on the loaded value guarding a loop (reduced to 46 instructions with DXC's dxa/dxopt). For them the size is read in the entry block and passed through groupshared memory across a group barrier, which hides where the offset came from. Every thread meets the barrier there. - One compute program and the eight glints pixel programs build once one clean-up pass runs (constant folding, or what a taken branch says about its condition). It runs for them whatever VICTORY_SHADER_OPT says. They are chosen by container digest, so every other program translates as before, and these compute the same values. Everything lives in emit/lower/amd.rs with a one-line hook where each change acts, so the change carries to forks. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01A752X7rKCzD2ViPbSWcyut --- crates/victory-shader/src/emit/bitcode.rs | 54 +++++++ crates/victory-shader/src/emit/lower.rs | 3 + crates/victory-shader/src/emit/lower/amd.rs | 148 ++++++++++++++++++++ crates/victory-shader/src/emit/mod.rs | 1 + crates/victory-shader/src/emit/shader.rs | 2 +- crates/victory-shader/src/lib.rs | 1 + 6 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 crates/victory-shader/src/emit/lower/amd.rs diff --git a/crates/victory-shader/src/emit/bitcode.rs b/crates/victory-shader/src/emit/bitcode.rs index eff0ac08a6..ea4d06ed98 100644 --- a/crates/victory-shader/src/emit/bitcode.rs +++ b/crates/victory-shader/src/emit/bitcode.rs @@ -408,6 +408,60 @@ impl Module { } } + /// A groupshared-style global named `prefix` and a number no other global has. + pub fn fresh_global(&mut self, prefix: &str, ty: Ty, space: u32) -> V { + let name = format!("{prefix}{}", self.globals.len()); + self.global(&name, ty, space) + } + + /// `v` computed at the end of `block`: itself where `block` already has it (or it is a + /// constant, global or function), else a copy of its integer arithmetic and casts over + /// such values. `None`, with nothing emitted, where it reads anything else. + pub fn rematerialize(&mut self, v: V, block: usize) -> Option { + if !self.copyable(v, block) { + return None; + } + Some(self.copy_into(v, block)) + } + + fn here(&self, v: V, block: usize) -> bool { + match v { + V::Inst(i) => self.blocks[block].body.contains(&i) || self.blocks[block].phis.contains(&i), + _ => true, + } + } + + fn copyable(&self, v: V, block: usize) -> bool { + if self.here(v, block) { + return true; + } + let V::Inst(i) = v else { return true }; + match self.insts[i as usize].op { + Op::Bin(_, a, b) => self.copyable(a, block) && self.copyable(b, block), + Op::Cast(_, a) => self.copyable(a, block), + _ => false, + } + } + + fn copy_into(&mut self, v: V, block: usize) -> V { + if self.here(v, block) { + return v; + } + let V::Inst(i) = v else { return v }; + let ty = self.insts[i as usize].ty; + match self.insts[i as usize].op { + Op::Bin(op, a, b) => { + let (a, b) = (self.copy_into(a, block), self.copy_into(b, block)); + self.bin(block, op, a, b) + } + Op::Cast(op, a) => { + let a = self.copy_into(a, block); + self.cast(block, op, a, ty) + } + _ => unreachable!("checked by copyable"), + } + } + fn push(&mut self, block: usize, ty: Ty, op: Op) -> V { let i = self.insts.len() as u32; self.insts.push(Inst { ty, op }); diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 718a9765c4..63cde42de9 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -6,6 +6,8 @@ //! emitter cannot express is `Missing` with a reason, and only fails the //! program when something with an effect (an export, a branch) needs it. +pub(super) mod amd; + use std::collections::{BTreeMap, HashMap, HashSet}; use super::UserData; @@ -5975,6 +5977,7 @@ impl<'a> Lower<'a> { .op(c, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]); // A raw view counts bytes; the descriptor's record count is in elements. let n = self.s.m.extract(c, d, 0); + let n = self.hoisted_size(&src, props)?.unwrap_or(n); // amd.rs let stride = used.map_or(0, |u| u.stride); let n = if props.0 & 0xFF == 11 && stride > 1 { let k = self.s.m.i32(stride); diff --git a/crates/victory-shader/src/emit/lower/amd.rs b/crates/victory-shader/src/emit/lower/amd.rs new file mode 100644 index 0000000000..4aa01ab931 --- /dev/null +++ b/crates/victory-shader/src/emit/lower/amd.rs @@ -0,0 +1,148 @@ +//! Changes to the translation of a few of the title's programs that keep AMD's shader +//! compiler from failing on them: amdxc64 8.18.10.0514 (Radeon RX 9060 XT) dereferences +//! null, or ends the process, creating their pipelines as otherwise translated. Found by +//! creating every program's pipeline (2,684 compute; 23,279 others with a stand-in partner +//! stage) in a process of its own. Chosen per program by container digest: every other +//! program translates as before, and these compute the same values, so a host without the +//! fault loses nothing. +//! +//! Kept to this file and a one-line hook where each change acts (`begin` before lowering, +//! `passes` in the clean-up, `hoisted_size` at a raw view's size), so it carries to forks. + +use std::cell::Cell; + +use super::super::bitcode::opt::{FACTS, FOLD}; +use super::super::bitcode::{Attr, V}; +use super::{Lower, Source, Why, meeting_mode}; + +#[derive(Clone, Copy)] +struct Fixes { + hoist_sizes: bool, + passes: u32, +} + +thread_local! { + /// The fixes of the program being lowered on this thread. + static CURRENT: Cell = const { Cell::new(Fixes { hoist_sizes: false, passes: 0 }) }; +} + +/// (container digest, hoist raw views' sizes, clean-up passes run whatever +/// `VICTORY_SHADER_OPT` says). +const FIXES: [(u128, bool, u32); 13] = [ + // Raw loads whose offset comes from the same view's size, a branch on the loaded value + // guarding a loop (reduced to 46 instructions with DXC's dxa and dxopt). + // bvh_skinning:CSComputeSkinningTreeAnimVelocity_d2f99501_Wrapped + (0x6177_8417_b3a9_0c25_203c_4842_27fe_ebfc, true, 0), + // GpuParticleUpdateCS_UpdateSkinnedAttachMeshTreeParticles_d5e6e48d_Wrapped + (0x07d1_ccf3_3c9b_4b4c_7619_eaff_ba86_96ed, true, 0), + // rtdirectionalshadows:CS_RTShadowTrace32HQ_Tiled_bfd3b062_Wrapped + (0x7e9f_4d75_149c_5673_6246_56da_a620_e24b, true, 0), + // rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP_Tiled + (0xa62d_11a6_bef5_7571_a1ab_8e73_e10c_470b, true, 0), + // indirect_diffuse_lighting:CS_ScreenProbes_ActivateRays_d60186e6_Wrapped: with no + // clean-up the process ends (0xFFFFFFFF); folding constants alone is enough. + (0x5212_83e5_0211_e301_e7fa_ed9e_0299_dc22, false, FOLD), + // The glints pixel programs build once branches tell their conditions. + // PS_MIRROR_PBR_MATERIAL_CLEARCOAT_GLINTS + (0xe416_8a83_c5c6_ff23_73e3_4325_35ad_7c9d, false, FACTS), + // PS_MIRROR_PBR_MATERIAL_GLINTS + (0x3024_35a1_3c4c_b152_2a2c_e785_8fab_7e34, false, FACTS), + // PS_MIRROR_PBR_MATERIAL_TERRAIN_GLINTS + (0x60f0_c383_b6ba_41b5_895d_4a40_39e7_a781, false, FACTS), + // PS_MIRROR_PBR_MATERIAL_TERRAIN_GLINTS_TRANSLUCENCY + (0xb867_ed98_e402_b5f8_8844_35ad_5d48_5b36, false, FACTS), + // PS_STUDIOSHOT_PBR_MATERIAL_CLEARCOAT_GLINTS_LIGHTING + (0x8e32_45bb_242a_2e59_17c1_d2ce_d51c_7fc9, false, FACTS), + // PS_STUDIOSHOT_PBR_MATERIAL_GLINTS_LIGHTING + (0x5bbf_8eed_9a4f_5133_8b8d_2e36_cd9f_4b66, false, FACTS), + // PS_STUDIOSHOT_PBR_MATERIAL_TERRAIN_GLINTS_LIGHTING + (0x3c03_fca2_b4cc_b79b_fb87_eb51_81de_8a70, false, FACTS), + // PS_STUDIOSHOT_PBR_MATERIAL_TERRAIN_GLINTS_TRANSLUCENCY_LIGHTING + (0x3df1_012d_c24c_6daf_af0b_c2ce_8fc7_8b7b, false, FACTS), +]; + +/// Before lowering the program whose container digest is `digest` on this thread. +pub(in crate::emit) fn begin(digest: [u8; 16]) { + let d = u128::from_be_bytes(digest); + let (hoist_sizes, passes) = FIXES.iter().find(|f| f.0 == d).map_or((false, 0), |f| (f.1, f.2)); + CURRENT.set(Fixes { hoist_sizes, passes }); +} + +/// Clean-up passes the program being lowered gets whatever `VICTORY_SHADER_OPT` says. +pub(in crate::emit) fn passes() -> u32 { + CURRENT.get().passes +} + +impl Lower<'_> { + /// For a program that hoists sizes: the byte size of the raw view `src` names, read in + /// the entry block and passed through groupshared memory across a group barrier, then + /// loaded here. The barrier hides from the compiler that a load's offset came from that + /// view's own size; in the entry block every thread meets it. `None` (the size as read + /// at the use) outside compute and mesh, or where the view's heap index is not known in + /// the entry block. + pub(super) fn hoisted_size(&mut self, src: &Source, props: (u32, u32)) -> Result, Why> { + let Source::Loaded { load, first } = *src else { + return Ok(None); + }; + if !CURRENT.get().hoist_sizes || self.s.threads.is_none() || self.x.res.waterfall.loads.contains(&load) { + return Ok(None); + } + let entry = self.s.entry; + // As `handle` computes it here, then again in the entry block where it is + // arithmetic over values known there. + let index = self.heap_index(load, first, false)?; + let Some(index) = self.s.m.rematerialize(index, entry) else { + return Ok(None); + }; + let i32t = self.s.t.i32; + let h = self.s.heap_handle(entry, index, false, false, props); + let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]); + let mip = self.s.m.undef(i32t); + let d = self.s.op(entry, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]); + let n = self.s.m.extract(entry, d, 0); + let slot = self.s.m.fresh_global("view_size", i32t, 3); + self.s.m.store(entry, slot, n); + let mode = self.s.m.i32(meeting_mode()); + let void = self.s.t.void; + self.s.op(entry, "barrier", Attr::NoDuplicate, void, 80, &[mode]); + Ok(Some(self.s.m.load(self.cur, slot))) + } +} + +#[cfg(test)] +mod tests { + use super::super::super::bitcode::{Bin, Module}; + + #[test] + fn rematerialize_copies_arithmetic_over_entry_values_only() { + let mut m = Module::default(); + let i32t = m.int(32); + let (entry, later) = (m.new_block(), m.new_block()); + let g = m.global("g", i32t, 3); + let base = m.load(entry, g); + let (four, five) = (m.i32(4), m.i32(5)); + // In `later`: (base + 4) >> 5, and arithmetic over a value read there. + let sum = m.bin(later, Bin::Add, base, four); + let index = m.bin(later, Bin::LShr, sum, five); + let loaded = m.load(later, g); + let other = m.bin(later, Bin::Add, loaded, four); + assert_eq!(m.rematerialize(base, entry), Some(base)); + assert_eq!(m.rematerialize(four, entry), Some(four)); + let copy = m.rematerialize(index, entry).expect("arithmetic over an entry value"); + assert_ne!(copy, index); + // The copy is in the entry block already. + assert_eq!(m.rematerialize(copy, entry), Some(copy)); + assert_eq!(m.rematerialize(other, entry), None); + } + + #[test] + fn only_the_listed_programs_change() { + super::begin([0; 16]); + assert_eq!(super::passes(), 0); + assert!(!super::CURRENT.get().hoist_sizes); + super::begin(0x5212_83e5_0211_e301_e7fa_ed9e_0299_dc22u128.to_be_bytes()); + assert_eq!(super::passes(), super::FOLD); + super::begin(0x6177_8417_b3a9_0c25_203c_4842_27fe_ebfcu128.to_be_bytes()); + assert!(super::CURRENT.get().hoist_sizes); + } +} diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index 2f413de87d..a811cbbda7 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -777,6 +777,7 @@ fn emit_program( 0xba30_5538_e28d_414c_af39_a51a_4bc2_f308 | 0xd635_3bf6_df15_ff8e_c4aa_aec1_0dbf_3974 ) && matches!(native.io, crate::native::StageIo::Cs(cs) if cs.threads == [64, 1, 1]); + lower::amd::begin(c.digest); let l = lower::lower_program(&parts, box_rehydration, tiled_lighting_uint_srvs(c.digest), tiled_ambient(c, native))?; let wave_size = l.wave_size; let shader = HostShader { diff --git a/crates/victory-shader/src/emit/shader.rs b/crates/victory-shader/src/emit/shader.rs index 429a870146..b9610689b4 100644 --- a/crates/victory-shader/src/emit/shader.rs +++ b/crates/victory-shader/src/emit/shader.rs @@ -550,7 +550,7 @@ impl Shader { // The clean-up, before anything is declared about the code: a // feature whose last use it removed must not be declared (the // validator compares the flags with what the code uses). - let passes = super::shader_opt() & super::bitcode::opt::MODULE; + let passes = (super::shader_opt() | super::lower::amd::passes()) & super::bitcode::opt::MODULE; if passes != 0 { self.m.optimise(passes); let used = self.m.usage(); diff --git a/crates/victory-shader/src/lib.rs b/crates/victory-shader/src/lib.rs index 74838d0588..0805da8270 100644 --- a/crates/victory-shader/src/lib.rs +++ b/crates/victory-shader/src/lib.rs @@ -62,6 +62,7 @@ translation_inputs! { "emit/guest_address.rs", "emit/intersect.rs", "emit/lower.rs", + "emit/lower/amd.rs", "emit/mod.rs", "emit/own_bits.rs", "emit/packed_mask.rs", From 2152e55b0df382ff252e003a534c94ce2bbe3e5c Mon Sep 17 00:00:00 2001 From: Toucann Date: Wed, 25 Nov 2026 19:23:33 -0700 Subject: [PATCH 2/2] Ask a buffer's size through a non-uniform handle of its own, as fxceless/victory-fork#1213 does The four size-read programs crash because the size query and the buffer's loads use the same uniform heap handle. fxceless/victory-fork#1213 (IxMxAMAR) asks the size through a handle of its own, marked non-uniform and not counted in nonuniform_handles. On the RX 9060 XT that clears all four here too, without a digest list. It replaces the groupshared hoist and the Module helpers it needed. The nine programs that need one clean-up pass keep it by digest. All 13 programs validate and create their pipelines on the RX 9060 XT; cargo test -p victory-shader: 240 passed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01A752X7rKCzD2ViPbSWcyut --- crates/victory-shader/src/emit/bitcode.rs | 54 ------- crates/victory-shader/src/emit/lower.rs | 3 +- crates/victory-shader/src/emit/lower/amd.rs | 154 +++++++------------- 3 files changed, 52 insertions(+), 159 deletions(-) diff --git a/crates/victory-shader/src/emit/bitcode.rs b/crates/victory-shader/src/emit/bitcode.rs index ea4d06ed98..eff0ac08a6 100644 --- a/crates/victory-shader/src/emit/bitcode.rs +++ b/crates/victory-shader/src/emit/bitcode.rs @@ -408,60 +408,6 @@ impl Module { } } - /// A groupshared-style global named `prefix` and a number no other global has. - pub fn fresh_global(&mut self, prefix: &str, ty: Ty, space: u32) -> V { - let name = format!("{prefix}{}", self.globals.len()); - self.global(&name, ty, space) - } - - /// `v` computed at the end of `block`: itself where `block` already has it (or it is a - /// constant, global or function), else a copy of its integer arithmetic and casts over - /// such values. `None`, with nothing emitted, where it reads anything else. - pub fn rematerialize(&mut self, v: V, block: usize) -> Option { - if !self.copyable(v, block) { - return None; - } - Some(self.copy_into(v, block)) - } - - fn here(&self, v: V, block: usize) -> bool { - match v { - V::Inst(i) => self.blocks[block].body.contains(&i) || self.blocks[block].phis.contains(&i), - _ => true, - } - } - - fn copyable(&self, v: V, block: usize) -> bool { - if self.here(v, block) { - return true; - } - let V::Inst(i) = v else { return true }; - match self.insts[i as usize].op { - Op::Bin(_, a, b) => self.copyable(a, block) && self.copyable(b, block), - Op::Cast(_, a) => self.copyable(a, block), - _ => false, - } - } - - fn copy_into(&mut self, v: V, block: usize) -> V { - if self.here(v, block) { - return v; - } - let V::Inst(i) = v else { return v }; - let ty = self.insts[i as usize].ty; - match self.insts[i as usize].op { - Op::Bin(op, a, b) => { - let (a, b) = (self.copy_into(a, block), self.copy_into(b, block)); - self.bin(block, op, a, b) - } - Op::Cast(op, a) => { - let a = self.copy_into(a, block); - self.cast(block, op, a, ty) - } - _ => unreachable!("checked by copyable"), - } - } - fn push(&mut self, block: usize, ty: Ty, op: Op) -> V { let i = self.insts.len() as u32; self.insts.push(Inst { ty, op }); diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 63cde42de9..0ab7ad4114 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -5964,7 +5964,7 @@ impl<'a> Lower<'a> { } _ => return Err("a scalar memory read as data"), }; - let h = self.handle(site, &src, false, props)?; + let h = self.size_view_handle(site, first, &src, props, image.is_none())?; // amd.rs let words: Vec<(u32, V)> = match image { Some(kind) => self.image_size_words(h, kind, uav), None => { @@ -5977,7 +5977,6 @@ impl<'a> Lower<'a> { .op(c, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]); // A raw view counts bytes; the descriptor's record count is in elements. let n = self.s.m.extract(c, d, 0); - let n = self.hoisted_size(&src, props)?.unwrap_or(n); // amd.rs let stride = used.map_or(0, |u| u.stride); let n = if props.0 & 0xFF == 11 && stride > 1 { let k = self.s.m.i32(stride); diff --git a/crates/victory-shader/src/emit/lower/amd.rs b/crates/victory-shader/src/emit/lower/amd.rs index 4aa01ab931..e3e89a410b 100644 --- a/crates/victory-shader/src/emit/lower/amd.rs +++ b/crates/victory-shader/src/emit/lower/amd.rs @@ -1,148 +1,96 @@ -//! Changes to the translation of a few of the title's programs that keep AMD's shader -//! compiler from failing on them: amdxc64 8.18.10.0514 (Radeon RX 9060 XT) dereferences -//! null, or ends the process, creating their pipelines as otherwise translated. Found by -//! creating every program's pipeline (2,684 compute; 23,279 others with a stand-in partner -//! stage) in a process of its own. Chosen per program by container digest: every other -//! program translates as before, and these compute the same values, so a host without the -//! fault loses nothing. +//! Changes to the translation that keep AMD's shader compiler from failing on the title's +//! programs: amdxc64 8.18.10.0514 (Radeon RX 9060 XT / 9070 XT, driver 32.0.32029.2010) +//! dereferences null, or ends the process, creating some of their pipelines as otherwise +//! translated. Found by creating every program's pipeline (2,684 compute; 23,279 others with a +//! stand-in partner stage) in a process of its own. //! -//! Kept to this file and a one-line hook where each change acts (`begin` before lowering, -//! `passes` in the clean-up, `hoisted_size` at a raw view's size), so it carries to forks. +//! - A buffer's size is asked through a heap handle of its own, marked non-uniform +//! (`size_view_handle`; fxceless/victory-fork#1213): through the uniform handle the buffer's +//! loads use, the compiler crashes where a load's offset comes from that size. +//! - Nine programs build once one clean-up pass runs; it runs for them whatever +//! `VICTORY_SHADER_OPT` says (`passes`, by container digest). +//! +//! Kept to this file and a one-line hook where each change acts, so it carries to forks. use std::cell::Cell; +use super::super::bitcode::V; use super::super::bitcode::opt::{FACTS, FOLD}; -use super::super::bitcode::{Attr, V}; -use super::{Lower, Source, Why, meeting_mode}; - -#[derive(Clone, Copy)] -struct Fixes { - hoist_sizes: bool, - passes: u32, -} +use super::{Lower, Site, Source, Why}; thread_local! { - /// The fixes of the program being lowered on this thread. - static CURRENT: Cell = const { Cell::new(Fixes { hoist_sizes: false, passes: 0 }) }; + /// The clean-up passes of the program being lowered on this thread. + static PASSES: Cell = const { Cell::new(0) }; } -/// (container digest, hoist raw views' sizes, clean-up passes run whatever -/// `VICTORY_SHADER_OPT` says). -const FIXES: [(u128, bool, u32); 13] = [ - // Raw loads whose offset comes from the same view's size, a branch on the loaded value - // guarding a loop (reduced to 46 instructions with DXC's dxa and dxopt). - // bvh_skinning:CSComputeSkinningTreeAnimVelocity_d2f99501_Wrapped - (0x6177_8417_b3a9_0c25_203c_4842_27fe_ebfc, true, 0), - // GpuParticleUpdateCS_UpdateSkinnedAttachMeshTreeParticles_d5e6e48d_Wrapped - (0x07d1_ccf3_3c9b_4b4c_7619_eaff_ba86_96ed, true, 0), - // rtdirectionalshadows:CS_RTShadowTrace32HQ_Tiled_bfd3b062_Wrapped - (0x7e9f_4d75_149c_5673_6246_56da_a620_e24b, true, 0), - // rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver2RPP_Tiled - (0xa62d_11a6_bef5_7571_a1ab_8e73_e10c_470b, true, 0), +/// (container digest, clean-up passes run whatever `VICTORY_SHADER_OPT` says). +const FIXES: [(u128, u32); 9] = [ // indirect_diffuse_lighting:CS_ScreenProbes_ActivateRays_d60186e6_Wrapped: with no // clean-up the process ends (0xFFFFFFFF); folding constants alone is enough. - (0x5212_83e5_0211_e301_e7fa_ed9e_0299_dc22, false, FOLD), + (0x5212_83e5_0211_e301_e7fa_ed9e_0299_dc22, FOLD), // The glints pixel programs build once branches tell their conditions. // PS_MIRROR_PBR_MATERIAL_CLEARCOAT_GLINTS - (0xe416_8a83_c5c6_ff23_73e3_4325_35ad_7c9d, false, FACTS), + (0xe416_8a83_c5c6_ff23_73e3_4325_35ad_7c9d, FACTS), // PS_MIRROR_PBR_MATERIAL_GLINTS - (0x3024_35a1_3c4c_b152_2a2c_e785_8fab_7e34, false, FACTS), + (0x3024_35a1_3c4c_b152_2a2c_e785_8fab_7e34, FACTS), // PS_MIRROR_PBR_MATERIAL_TERRAIN_GLINTS - (0x60f0_c383_b6ba_41b5_895d_4a40_39e7_a781, false, FACTS), + (0x60f0_c383_b6ba_41b5_895d_4a40_39e7_a781, FACTS), // PS_MIRROR_PBR_MATERIAL_TERRAIN_GLINTS_TRANSLUCENCY - (0xb867_ed98_e402_b5f8_8844_35ad_5d48_5b36, false, FACTS), + (0xb867_ed98_e402_b5f8_8844_35ad_5d48_5b36, FACTS), // PS_STUDIOSHOT_PBR_MATERIAL_CLEARCOAT_GLINTS_LIGHTING - (0x8e32_45bb_242a_2e59_17c1_d2ce_d51c_7fc9, false, FACTS), + (0x8e32_45bb_242a_2e59_17c1_d2ce_d51c_7fc9, FACTS), // PS_STUDIOSHOT_PBR_MATERIAL_GLINTS_LIGHTING - (0x5bbf_8eed_9a4f_5133_8b8d_2e36_cd9f_4b66, false, FACTS), + (0x5bbf_8eed_9a4f_5133_8b8d_2e36_cd9f_4b66, FACTS), // PS_STUDIOSHOT_PBR_MATERIAL_TERRAIN_GLINTS_LIGHTING - (0x3c03_fca2_b4cc_b79b_fb87_eb51_81de_8a70, false, FACTS), + (0x3c03_fca2_b4cc_b79b_fb87_eb51_81de_8a70, FACTS), // PS_STUDIOSHOT_PBR_MATERIAL_TERRAIN_GLINTS_TRANSLUCENCY_LIGHTING - (0x3df1_012d_c24c_6daf_af0b_c2ce_8fc7_8b7b, false, FACTS), + (0x3df1_012d_c24c_6daf_af0b_c2ce_8fc7_8b7b, FACTS), ]; /// Before lowering the program whose container digest is `digest` on this thread. pub(in crate::emit) fn begin(digest: [u8; 16]) { let d = u128::from_be_bytes(digest); - let (hoist_sizes, passes) = FIXES.iter().find(|f| f.0 == d).map_or((false, 0), |f| (f.1, f.2)); - CURRENT.set(Fixes { hoist_sizes, passes }); + PASSES.set(FIXES.iter().find(|f| f.0 == d).map_or(0, |f| f.1)); } /// Clean-up passes the program being lowered gets whatever `VICTORY_SHADER_OPT` says. pub(in crate::emit) fn passes() -> u32 { - CURRENT.get().passes + PASSES.get() } impl Lower<'_> { - /// For a program that hoists sizes: the byte size of the raw view `src` names, read in - /// the entry block and passed through groupshared memory across a group barrier, then - /// loaded here. The barrier hides from the compiler that a load's offset came from that - /// view's own size; in the entry block every thread meets it. `None` (the size as read - /// at the use) outside compute and mesh, or where the view's heap index is not known in - /// the entry block. - pub(super) fn hoisted_size(&mut self, src: &Source, props: (u32, u32)) -> Result, Why> { - let Source::Loaded { load, first } = *src else { - return Ok(None); - }; - if !CURRENT.get().hoist_sizes || self.s.threads.is_none() || self.x.res.waterfall.loads.contains(&load) { - return Ok(None); + /// 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. AMD's compiler reads a null pointer compiling the query through the + /// uniform handle the buffer's loads use (fxceless/victory-fork#1213, IxMxAMAR). + 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 entry = self.s.entry; - // As `handle` computes it here, then again in the entry block where it is - // arithmetic over values known there. - let index = self.heap_index(load, first, false)?; - let Some(index) = self.s.m.rematerialize(index, entry) else { - return Ok(None); - }; - let i32t = self.s.t.i32; - let h = self.s.heap_handle(entry, index, false, false, props); - let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]); - let mip = self.s.m.undef(i32t); - let d = self.s.op(entry, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]); - let n = self.s.m.extract(entry, d, 0); - let slot = self.s.m.fresh_global("view_size", i32t, 3); - self.s.m.store(entry, slot, n); - let mode = self.s.m.i32(meeting_mode()); - let void = self.s.t.void; - self.s.op(entry, "barrier", Attr::NoDuplicate, void, 80, &[mode]); - Ok(Some(self.s.m.load(self.cur, slot))) + let index = self.heap_index(site, first, false)?; + let h = self.s.heap_handle(self.cur, index, false, true, props); + self.s.nonuniform_handles -= 1; + Ok(h) } } #[cfg(test)] mod tests { - use super::super::super::bitcode::{Bin, Module}; - - #[test] - fn rematerialize_copies_arithmetic_over_entry_values_only() { - let mut m = Module::default(); - let i32t = m.int(32); - let (entry, later) = (m.new_block(), m.new_block()); - let g = m.global("g", i32t, 3); - let base = m.load(entry, g); - let (four, five) = (m.i32(4), m.i32(5)); - // In `later`: (base + 4) >> 5, and arithmetic over a value read there. - let sum = m.bin(later, Bin::Add, base, four); - let index = m.bin(later, Bin::LShr, sum, five); - let loaded = m.load(later, g); - let other = m.bin(later, Bin::Add, loaded, four); - assert_eq!(m.rematerialize(base, entry), Some(base)); - assert_eq!(m.rematerialize(four, entry), Some(four)); - let copy = m.rematerialize(index, entry).expect("arithmetic over an entry value"); - assert_ne!(copy, index); - // The copy is in the entry block already. - assert_eq!(m.rematerialize(copy, entry), Some(copy)); - assert_eq!(m.rematerialize(other, entry), None); - } - #[test] - fn only_the_listed_programs_change() { + fn only_the_listed_programs_get_passes() { super::begin([0; 16]); assert_eq!(super::passes(), 0); - assert!(!super::CURRENT.get().hoist_sizes); super::begin(0x5212_83e5_0211_e301_e7fa_ed9e_0299_dc22u128.to_be_bytes()); assert_eq!(super::passes(), super::FOLD); - super::begin(0x6177_8417_b3a9_0c25_203c_4842_27fe_ebfcu128.to_be_bytes()); - assert!(super::CURRENT.get().hoist_sizes); + super::begin(0x3024_35a1_3c4c_b152_2a2c_e785_8fab_7e34u128.to_be_bytes()); + assert_eq!(super::passes(), super::FACTS); } }