From 7a84e7a7d6b5d4ce50302fd2a8bf65fdd1b0ba54 Mon Sep 17 00:00:00 2001 From: bmjask <11672696+bmjask@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:07:15 -0500 Subject: [PATCH] Direct DXIL: image_get_lod with a static sampler; slowest programs in the sweep report image_get_lod rejected MIMGs whose S# the console built from constants (79 PS of the GTA VI pack, the only non-geometry gap of a full sweep). The static/heap sampler choice moves into Lw::mimg_sampler, shared by sample/gather and get_lod. The coverage report also keeps the 10 slowest programs (text output), to chase the translation-time tail. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/core_tests.rs | 27 +++++++++++++++ crates/xcl/src/gpu/dxil/coverage.rs | 17 ++++++++- crates/xcl/src/gpu/dxil/lower_image.rs | 48 +++++++++++++------------- 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index c938b38624..9e872ccaba 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -356,3 +356,30 @@ fn multi_window_mem_format_and_bvh_validate() { } } } + +/// `image_get_lod` with a static sampler (an S# built from constants in the code) translates +/// to `CalculateLevelOfDetail` on the sampler bound at `s, space1`, shared with a sample of +/// the same S#, and validates. +#[test] +fn ps_get_lod_with_a_static_sampler() { + use crate::gpu::recomp::{Mode, PsInterface}; + let clamp = 2 | 2 << 3 | 2 << 6; + let linear = [clamp, 0x00FF_F000u32, 1 << 20 | 1 << 22, 0]; + let mut list: Vec<(&str, &str, Vec<(&str, u64)>, Option)> = Vec::new(); + for (k, &w) in linear.iter().enumerate() { + list.push(if w == 0 { ("S_MOV_B32", "ENC_SOP1", vec![("SDST", 24 + k as u64), ("SSRC0", 128)], None) } else { ("S_MOV_B32", "ENC_SOP1", vec![("SDST", 24 + k as u64), ("SSRC0", 255)], Some(w)) }); + } + // uv = (I, J) of the first barycentric pair (v0, v1); T# s[16:23], S# s[24:27] + list.push(("IMAGE_GET_LOD", "ENC_MIMG", vec![("VDATA", 4), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x3), ("DIM", 1)], None)); + list.push(("IMAGE_SAMPLE", "ENC_MIMG", vec![("VDATA", 6), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x3), ("DIM", 1)], None)); + list.push(("EXP", "ENC_EXP", vec![("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 5), ("VSRC2", 6), ("VSRC3", 7), ("DONE", 1), ("VM", 1)], None)); + let refs: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); + let code = prog(&refs); + let psi = PsInterface { inputs: vec![sig("SV_Position", 0, 1, 3, 0, 0xF)], outputs: vec![sig("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x2, attrs: None }; + let ko = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }; + let k = super::kernel::translate_pixel(&code, ko, &psi).unwrap_or_else(|e| panic!("{}", &e[..e.len().min(800)])); + assert_eq!(k.static_samplers, vec![(linear, false)], "one static sampler, shared by get_lod and sample"); + if crate::gpu::recomp::dxc::location().is_ok() { + super::coverage::translate_mode(&code, &Mode::Pixel(psi), ko, true).unwrap_or_else(|e| panic!("{}", &e[..e.len().min(800)])); + } +} diff --git a/crates/xcl/src/gpu/dxil/coverage.rs b/crates/xcl/src/gpu/dxil/coverage.rs index 62c7a84ec7..f1aa9b2db5 100644 --- a/crates/xcl/src/gpu/dxil/coverage.rs +++ b/crates/xcl/src/gpu/dxil/coverage.rs @@ -123,12 +123,23 @@ pub struct Report { pub times_us: Vec, /// (direct DXIL bytes, HLSL-path DXIL bytes) for programs both paths produced pub sizes: Vec<(usize, usize)>, + /// the slowest programs: (microseconds, program), slowest first, at most [`SLOWEST`] + pub slowest: Vec<(u64, String)>, } +/// How many of the slowest programs a [`Report`] keeps. +pub const SLOWEST: usize = 10; + impl Report { pub fn add(&mut self, name: &str, s: &Sample) { let c = self.stages.entry(s.stage).or_default(); - self.times_us.push(s.time.as_micros() as u64); + let us = s.time.as_micros() as u64; + self.times_us.push(us); + if self.slowest.len() < SLOWEST || self.slowest.last().is_some_and(|l| l.0 < us) { + let at = self.slowest.partition_point(|l| l.0 >= us); + self.slowest.insert(at, (us, name.to_string())); + self.slowest.truncate(SLOWEST); + } match &s.result { Ok(size) => { c.translated += 1; @@ -186,6 +197,10 @@ impl Report { row(&mut s, "all", &self.total()); let (p50, p95, max) = self.time_percentiles(); s.push_str(&format!(" translation time per program: p50 {:.2} ms, p95 {:.2} ms, max {:.2} ms\n", p50 as f64 / 1e3, p95 as f64 / 1e3, max as f64 / 1e3)); + for (us, name) in &self.slowest { + s.push_str(&format!(" slow: {:>9.2} ms {name} +", *us as f64 / 1e3)); + } if !self.sizes.is_empty() { let (d, h): (usize, usize) = self.sizes.iter().fold((0, 0), |a, b| (a.0 + b.0, a.1 + b.1)); s.push_str(&format!(" DXIL size, {} programs: direct {} B, HLSL+DXC {} B (direct/HLSL {:.2})\n", self.sizes.len(), d, h, d as f64 / h.max(1) as f64)); diff --git a/crates/xcl/src/gpu/dxil/lower_image.rs b/crates/xcl/src/gpu/dxil/lower_image.rs index 9a588e9c18..9acee38a69 100644 --- a/crates/xcl/src/gpu/dxil/lower_image.rs +++ b/crates/xcl/src/gpu/dxil/lower_image.rs @@ -12,6 +12,28 @@ use super::ops_core::op; use crate::gpu::rdna2::{self, Decoded}; impl Lw { + /// The sampler of the MIMG at `pc`: a static sampler (bound at `s, space1` in order of + /// first use) when the console bound one there, else the heap sampler the S# indexes. + fn mimg_sampler(&mut self, pc: u64, has_c: bool, s3: Value) -> Sampler { + match self.static_samplers.get(&pc).copied() { + Some(w) => match self.static_used.iter().find(|u| u.0 == w && u.1 == has_c) { + Some(u) => u.2, + None => { + let k = self.static_used.len() as u32; + let cur = self.cur(); + self.s.m.set_block(self.init.entry); + let h = self.s.sampler(self.sampler_base + k, 1, has_c); + self.s.m.set_block(cur); + self.static_used.push((w, has_c, h)); + h + } + }, + None => { + let si = self.andc(s3, 0xFFF); + self.s.heap_sampler(si, has_c, true) + } + } + } fn f3(&mut self, a: [Value; 3], b: [Value; 3], op: fn(&mut Lw, Value, Value) -> Value) -> [Value; 3] { [op(self, a[0], b[0]), op(self, a[1], b[1]), op(self, a[2], b[2])] } @@ -466,25 +488,7 @@ impl Lw { let ps = self.gfx.as_ref().is_some_and(|g| g.ps.is_some()); let spatial: Vec = coords[..kind.grads().min(coords.len())].to_vec(); let tex = self.s.heap_texture(tw0, kind, false, CompType::F32, 4, true); - let smp: Sampler = match self.static_samplers.get(&d.pc).copied() { - // the console's static samplers: bound at s, space1 in order of first use - Some(w) => match self.static_used.iter().find(|u| u.0 == w && u.1 == has_c) { - Some(u) => u.2, - None => { - let k = self.static_used.len() as u32; - let cur = self.cur(); - self.s.m.set_block(self.init.entry); - let h = self.s.sampler(self.sampler_base + k, 1, has_c); - self.s.m.set_block(cur); - self.static_used.push((w, has_c, h)); - h - } - }, - None => { - let si = self.andc(s3, 0xFFF); - self.s.heap_sampler(si, has_c, true) - } - }; + let smp: Sampler = self.mimg_sampler(d.pc, has_c, s3); if let Some(o) = ovgpr { // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given, else 0) let level = match (&lod, has_lz || gather) { @@ -620,9 +624,6 @@ impl Lw { if !self.gfx.as_ref().is_some_and(|g| g.ps.is_some()) { return err(d, "image_get_lod outside PS"); } - if self.static_samplers.contains_key(&d.pc) { - return err(d, "image_get_lod with a static sampler"); - } let coords: Vec = (0..body as usize).map(|k| compf(self, i_body + k)).collect(); let spatial: Vec = match dim { 3 => self.cube_dir(coords[0], coords[1], coords[2]).to_vec(), @@ -630,8 +631,7 @@ impl Lw { }; let s3 = self.sreg(ssamp + 3); let tex = self.s.heap_texture(tw0, kind, false, CompType::F32, 4, true); - let si = self.andc(s3, 0xFFF); - let smp = self.s.heap_sampler(si, false, true); + let smp = self.mimg_sampler(d.pc, false, s3); let a = self.s.calculate_lod(tex, smp, &spatial, true); let b = self.s.calculate_lod(tex, smp, &spatial, false); let z = self.c(0);