Skip to content
Merged
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
27 changes: 27 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i>, 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<u32>)> = 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<u32>)> = 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)]));
}
}
17 changes: 16 additions & 1 deletion crates/xcl/src/gpu/dxil/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,23 @@ pub struct Report {
pub times_us: Vec<u64>,
/// (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;
Expand Down Expand Up @@ -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));
Expand Down
48 changes: 24 additions & 24 deletions crates/xcl/src/gpu/dxil/lower_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i>, 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])]
}
Expand Down Expand Up @@ -466,25 +488,7 @@ impl Lw {
let ps = self.gfx.as_ref().is_some_and(|g| g.ps.is_some());
let spatial: Vec<Value> = 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<i>, 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) {
Expand Down Expand Up @@ -620,18 +624,14 @@ 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<Value> = (0..body as usize).map(|k| compf(self, i_body + k)).collect();
let spatial: Vec<Value> = match dim {
3 => self.cube_dir(coords[0], coords[1], coords[2]).to_vec(),
_ => coords[..kind.grads().min(coords.len())].to_vec(),
};
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);
Expand Down
Loading