From 38410b88abfaa81c24b40b39aeeabc59bda937e9 Mon Sep 17 00:00:00 2001 From: local Date: Wed, 25 Nov 2026 01:12:28 -0600 Subject: [PATCH] VIctory e29391f, 13185f5 and 4fc9d70, by hand 16-bit interpolations write the half op_sel picks, with omod and clamp; the device says whether it was removed; detiling ends with its staging buffer. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/device.rs | 14 +++ crates/victory-gpu/src/texture.rs | 6 +- crates/victory-shader/src/emit/lower.rs | 18 +++- crates/victory-shader/src/emit/mod.rs | 121 ++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 2 deletions(-) diff --git a/crates/victory-gpu/src/device.rs b/crates/victory-gpu/src/device.rs index dfbf2b2..569bafd 100644 --- a/crates/victory-gpu/src/device.rs +++ b/crates/victory-gpu/src/device.rs @@ -399,6 +399,19 @@ extern "system" fn unknown_pipeline( E_NOTIMPL } +/// The host device's state: the console's device is removed when the host's is +/// (VIctory 13185f5: the default answer, E_NOTIMPL, reads as a lost device). +extern "system" fn removed_reason(_: This) -> HRESULT { + #[cfg(windows)] + if let Some(host) = crate::host::host() { + // SAFETY: a plain query. + if let Err(e) = unsafe { host.device.GetDeviceRemovedReason() } { + return e.code().0; + } + } + S_OK +} + /// IUnknown 3, object methods 4, device methods 76 (0x18-0x290), then the /// methods `ID3D12Device1` to `ID3D12Device8` add, in order: 3, 1, 3, 6, 9, /// 1, 2, 9 (0x298-0x3a0, the console's own among them), then those of @@ -431,6 +444,7 @@ static FUNCTIONS: Table<160> = { f[28] = resource::create_heap as *const (); // 0xe0 f[29] = resource::create_placed as *const (); // 0xe8 f[36] = fence::create as *const (); // 0x120 + f[37] = removed_reason as *const (); // 0x128 f[38] = resource::copyable_footprints as *const (); // 0x130 f[39] = query::create as *const (); // 0x138 f[41] = signature::create as *const (); // 0x148 diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 56f2726..f3c2a8d 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -1243,7 +1243,11 @@ impl Staging { let (mip, slice) = (i as u32 % self.mips, i as u32 / self.mips); let f = footprint.Footprint; let pitch = f.RowPitch as usize; - let len = pitch * rows as usize * f.Depth as usize; + // The last row of the last subresource is not padded to its pitch + // (VIctory 4fc9d70): to the end of the buffer at most, where a + // block of the pool goes on with another texture's. + let len = (pitch * rows as usize * f.Depth as usize) + .min(self.bytes.saturating_sub(footprint.Offset) as usize); // SAFETY: the subresource's part of the mapped staging buffer. let linear = unsafe { std::slice::from_raw_parts_mut(self.data.add(footprint.Offset as usize), len) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 4672121..d4b0163 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -9927,7 +9927,23 @@ impl<'a> Lower<'a> { let f = half(self, h)?.ok_or("a packed input half no element holds")?; self.as_int(f) } else if f16 { - let f = half(self, h)?.ok_or("a packed input half no element holds")?; + let mut f = half(self, h)?.ok_or("a packed input half no element holds")?; + if let Fields::Valu(v) = &inst.fields { + // omod, then clamp, before the one rounding to 16 bits; op_sel[3] + // picks the destination half (VIctory e29391f: the low half was + // always written, the high one's value never arrived). + let (omod, clamp) = (v.omod, v.clamp); + if omod != 0 { + let factor = [1.0f32, 2.0, 4.0, 0.5][usize::from(omod)]; + let k = self.s.m.f32(factor.to_bits()); + f = self.s.m.bin(c, Bin::Mul, f, k); + } + if clamp { + f = self.unary(7, f); + } + let lo = to_f16(self, f); + return self.write_half(inst, lo, None); + } let lo = to_f16(self, f); self.low_half(inst, reg, lo) } else if matches!(eval, Eval::Mode(1)) { diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index 454689e..b9f52c4 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -816,6 +816,127 @@ mod tests { use super::lower::LOOP_LIMIT; use super::parse_loop_limit; + /// The LLVM text `dxc -dumpbin` prints for a DXIL container, when dxc + /// is installed. + #[cfg(windows)] + fn dump_dxil(container: &[u8]) -> Option { + let dxc = match std::env::var_os("VICTORY_PC_DXC") { + Some(p) => std::path::PathBuf::from(p), + None => super::validator::Validator::default_path()?.with_file_name("dxc.exe"), + }; + if !dxc.exists() { + return None; + } + let file = std::env::temp_dir().join(format!("victory-interp-{}.dxil", std::process::id())); + std::fs::write(&file, container).ok()?; + let out = std::process::Command::new(dxc) + .arg("-dumpbin") + .arg(&file) + .output() + .ok()?; + let _ = std::fs::remove_file(&file); + assert!( + out.status.success(), + "dxc -dumpbin failed: {}", + String::from_utf8_lossy(&out.stderr) + ); + Some(String::from_utf8_lossy(&out.stdout).into_owned()) + } + + #[cfg(windows)] + #[test] + fn interpolated_f16_writes_the_op_sel_half_with_modifiers() { + // p1_f32 v2, v0, attr0.x; then a p2_f16 completing it into v3, which holds a value + // (v2's own is no number here: a first half is evaluated with its second). + let p2 = |dst_high: bool, omod: u32, clamp: bool| { + let vgpr = |n: u32| 256 + n; + let mut w0 = 0x35u32 << 26 | 0x35au32 << 16 | 3; + if dst_high { + w0 |= 1 << 14; // op_sel[3]: the high destination half. + } + if clamp { + w0 |= 1 << 15; + } + let w1 = vgpr(1) << 9 | vgpr(2) << 18; + [w0, w1 | omod << 27] + }; + let dump = |dst_high: bool, omod: u32, clamp: bool| { + let [w0, w1] = p2(dst_high, omod, clamp); + let words = [ + 0x7E06_0301u32, // v_mov_b32 v3, v1 + 0xC808_0000, // v_interp_p1_f32 v2, v0, attr0.x + w0, + w1, // v_interp_p2_f16 v3, attr0, v1, v2 + 0xF800_180F, + 0x0303_0303, // exp mrt0, v3, v3, v3, v3 done vm + 0xBF81_0000, + ]; + let native = super::NativeProgram { + stage: super::HwStage::Ps, + wave64: false, + vgprs: 4, + layout: vec![], + used: vec![], + io: crate::native::StageIo::Ps { + inputs: vec![crate::native::PsInput { + usage: crate::native::Usage::Generic, + row: 0, + mask: 0b1111, + attr: 0, + flat: false, + fp16: true, + pack2: false, + row2: 0, + }], + input_addr: 2, + db_shader_control: 0, + patches: vec![], + }, + code: words.iter().flat_map(|w| w.to_le_bytes()).collect(), + }; + let el = |name: &str, kind, usage, half| SigElement { + name: name.into(), + index: 0, + kind, + comp: 9, + interp: 0, + row: Some(0), + col: 0, + cols: 4, + usage, + half, + }; + let isg1 = super::container::signature(&[el("TEXCOORD", 0, 0, true)], true); + let osg1 = super::container::signature(&[el("SV_Target", 16, 15, false)], false); + let bytes = + crate::dxbc::build(&[(super::FourCC::ISG1, &isg1), (super::FourCC::OSG1, &osg1)]); + let c = super::Container::parse(&bytes).unwrap(); + let p = crate::ir::build(&native.patched_code(), native.wave64, native.vgprs).unwrap(); + let st = crate::structure::structure(&p).unwrap(); + let res = crate::resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap(); + let io = crate::io::raise(&p, &native).unwrap(); + super::emit(&c, &native, &p, &st, &io, &res).unwrap().dxil + }; + let (Some(low), Some(high)) = ( + dump_dxil(&dump(false, 0, false)), + dump_dxil(&dump(true, 1, true)), + ) else { + eprintln!("no dxc.exe; DXIL check skipped"); + return; + }; + // The low-half write keeps the destination's high half. + assert!(low.contains("-65536"), "low write keeps the high half"); + // The high-half write shifts the result up by 16. + assert!(high.contains("shl i32"), "op_sel[3] writes the high half"); + // omod mul:2 and clamp (dx.op.unary.f32, opcode 7) are applied. + assert!(high.contains("2.000000e+00"), "omod multiplies by two"); + assert!( + high.contains("dx.op.unary.f32(i32 7"), + "clamp saturates the result" + ); + assert!(!low.contains("dx.op.unary.f32(i32 7")); + } + #[test] fn the_clean_up_switch_parses_or_runs_every_pass() { let all = super::bitcode::opt::ALL;