From 54269d7d7a734d0abec4738b963e7c7684b25e3c Mon Sep 17 00:00:00 2001 From: mastrr9 <333127948+mastrr9@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:35:14 +0000 Subject: [PATCH] dxil: image_sample_o under implicit LOD offsets each mip by its texels The HLSL path's image_sample_o in a pixel shader with an implicit LOD and a linear mip filter samples the two blended mips with SampleLevel, each moved by the offset in its own texels, and blends them by the LOD's fraction (20271d46). The direct translator still moved the coordinates once, by the offset over the size of the rounded LOD's mip, so the second mip read a texel off when the offset was half its texel or less. Port that form: LOD = the clamped implicit LOD (raised by the _cl clamp, capped at the view's last mip), mips floor(LOD) and the next one (capped too), each with its own texel offset, lerp by the fraction. It is taken under the same conditions read from the S#: MIP_FILTER linear, not anisotropic, and LOD >= 1 or min = mag filter; otherwise the single Sample as before. The texel-offset computation moves into a helper shared with the existing path. TRANSLATOR_VERSION 7 -> 8: the emitted containers change for these programs. Open PR #1793 also bumps TRANSLATOR_VERSION. Identical edits merge silently in git, so check the bump by hand at merge time: the later change takes the next free number, or the pipeline cache could serve containers of another translator under the same version. The existing test gpu_ps_image_sample_offset_implicit_lod_between_mips now also draws its pixel shader through the direct translator and expects the same result. --- crates/xcl/src/gpu/dxil/kernel.rs | 2 +- crates/xcl/src/gpu/dxil/lower_image.rs | 130 +++++++++++++++++++++---- crates/xcl/src/gpu/recomp/tests.rs | 33 +++++-- 3 files changed, 138 insertions(+), 27 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index d28a2ffcb6..fb9d78e1a1 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -37,7 +37,7 @@ pub struct KernelOptions { /// Version of the direct translator's output, part of the on-disk translation cache's /// identity (`recomp::pso`): bump it on any change that alters the emitted containers /// (the `gpu/dxil` sources are not in build.rs's translator source hash). -pub const TRANSLATOR_VERSION: u32 = 7; +pub const TRANSLATOR_VERSION: u32 = 8; /// `XCL_DIRECT_DXIL=1`: console compute shaders (and the kernel tests) go through this /// translator, with the HLSL path as the fallback for what it cannot translate yet. diff --git a/crates/xcl/src/gpu/dxil/lower_image.rs b/crates/xcl/src/gpu/dxil/lower_image.rs index 55767084b5..c5cd0fa265 100644 --- a/crates/xcl/src/gpu/dxil/lower_image.rs +++ b/crates/xcl/src/gpu/dxil/lower_image.rs @@ -270,6 +270,30 @@ impl Lw { self.uitof(c) } + /// An MIMG texel offset (`o`: three 6-bit signed fields) in the texture coordinates of + /// mip `level` – offset / that level's size (array layers do not move) – and the + /// level's `getDimensions`. + fn texel_offset(&mut self, tex: Texture, dim: u64, o: Value, level: Value) -> ([Value; 3], Value) { + let dims = self.s.get_dimensions(tex.handle, Some(level)); + let one = self.c(1); + let (w, hh, dd) = match dim { + 0 => (self.s.m.extract(dims, 0), one, one), + 1 => (self.s.m.extract(dims, 0), self.s.m.extract(dims, 1), one), + 2 => (self.s.m.extract(dims, 0), self.s.m.extract(dims, 1), self.s.m.extract(dims, 2)), + 4 => (self.s.m.extract(dims, 0), one, one), + _ => (self.s.m.extract(dims, 0), self.s.m.extract(dims, 1), one), + }; + let mut ofs = [one; 3]; + for (k, (sh, size)) in [(26u32, w), (18, hh), (10, dd)].into_iter().enumerate() { + let x = self.shlc(o, sh); + let x = self.ashrc(x, 26); + let xf = self.sitof(x); + let sf = self.uitof(size); + ofs[k] = self.fdiv(xf, sf); + } + (ofs, dims) + } + pub fn mimg(&mut self, d: &Decoded, name: &str) -> LResult<()> { let f = |n: &str| rdna2::field_value(d.enc, &d.words, n).unwrap_or(0); if f("TFE") != 0 || f("LWE") != 0 { @@ -488,6 +512,8 @@ impl Lw { None }; let mut coords: Vec = (0..body as usize).map(|k| compf(self, i_body + k)).collect(); + let coords_unmoved = coords.clone(); + let mut ofs_dims = None; let lod = i_extra.map(|i| compf(self, i)); let cl = i_clamp.map(|i| compf(self, i)); let s3 = self.sreg(ssamp + 3); @@ -524,23 +550,8 @@ impl Lw { } _ => self.c(0), }; - let dims = self.s.get_dimensions(tex.handle, Some(level)); - let one = self.c(1); - let (w, hh, dd) = match dim { - 0 => (self.s.m.extract(dims, 0), one, one), - 1 => (self.s.m.extract(dims, 0), self.s.m.extract(dims, 1), one), - 2 => (self.s.m.extract(dims, 0), self.s.m.extract(dims, 1), self.s.m.extract(dims, 2)), - 4 => (self.s.m.extract(dims, 0), one, one), - _ => (self.s.m.extract(dims, 0), self.s.m.extract(dims, 1), one), - }; - let mut ofs = Vec::new(); - for (sh, size) in [(26u32, w), (18, hh), (10, dd)] { - let x = self.shlc(o, sh); - let x = self.ashrc(x, 26); - let xf = self.sitof(x); - let sf = self.uitof(size); - ofs.push(self.fdiv(xf, sf)); - } + let (ofs, dims) = self.texel_offset(tex, dim, o, level); + ofs_dims = Some(dims); for (k, c) in coords.iter_mut().enumerate() { if (dim == 4 && k == 1) || (dim == 5 && k == 2) { continue; @@ -610,6 +621,91 @@ impl Lw { let l = floor(self, *b); self.s.sample_level(tex, smp, &coord, &[], l) } + // implicit LOD with an offset and a linear mip filter: the console moves each of + // the two blended mips by the offset in its own texels – sampled per mip and + // blended here (PS, not lz). Anisotropic or point-mip samplers keep the one-mip + // form of the last arm. + _ if ovgpr.is_some() && ps && !has_lz => { + let o = ovgpr.expect("offset"); + // the view's levels; a clamp past the last mip samples the last mip + let n = self.s.m.extract(ofs_dims.expect("offset dims"), 3); + let one = self.c(1); + let n = self.umax(n, one); + let last = self.sub(n, one); + let l = self.s.calculate_lod(tex, smp, &spatial, true); + let l = floor(self, l); + let lastf = self.uitof(last); + let l = self.fmin(l, lastf); + let l0 = self.un(op::ROUND_NI, l); + let l0u = self.ftoui(l0); + let l1u = self.addc(l0u, 1); + let l1u = self.umin(l1u, last); + let l1 = self.uitof(l1u); + let per_mip = |lw: &mut Lw, level: Value, lf: Value| { + let (ofs, _) = lw.texel_offset(tex, dim, o, level); + let mut c = coords_unmoved.clone(); + for (k, x) in c.iter_mut().enumerate() { + if !((dim == 4 && k == 1) || (dim == 5 && k == 2)) { + *x = lw.fadd(*x, ofs[k]); + } + } + match dim { + 4 => c[1] = lw.un(op::ROUND_NE, c[1]), + 5 => c[2] = lw.un(op::ROUND_NE, c[2]), + _ => {} + } + lw.s.sample_level(tex, smp, &c, &[], lf) + }; + let m0 = per_mip(self, l0u, l0); + let m1 = per_mip(self, l1u, l1); + let s1 = self.s.sample(tex, smp, &coord, &[], cl); + // S# word 2: XY_MAG_FILTER [21:20], XY_MIN_FILTER [23:22], MIP_FILTER [27:26]; + // word 0: MAX_ANISO_RATIO [11:9] – anisotropic only with an aniso min / mag + // filter, as `desc::sampler_d3d12` decides. A level-0 SampleLevel filters with + // the mag filter, so mip 0 of a minified sample needs min = mag. + let (w0, w2) = match self.static_samplers.get(&d.pc).copied() { + Some(w) => (self.c(w[0]), self.c(w[2])), + None => (self.sreg(ssamp), self.sreg(ssamp + 2)), + }; + let field = |lw: &mut Lw, w: Value, sh: u32, m: u32| { + let x = lw.lshrc(w, sh); + lw.andc(x, m) + }; + let mag = field(self, w2, 20, 3); + let min = field(self, w2, 22, 3); + let mip = field(self, w2, 26, 3); + let ratio = field(self, w0, 9, 7); + let linear = self.cmpc(Pred::Eq, mip, 2); + let mag_a = self.cmpc(Pred::UGe, mag, 2); + let min_a = self.cmpc(Pred::UGe, min, 2); + let a = self.lor(mag_a, min_a); + let r = self.nz(ratio); + let aniso = self.land(a, r); + let k1 = self.fc(1.0); + let up = self.fcmp(Pred::FOGe, l0, k1); + // a filter's linear bit: point 0, linear 1, aniso point 2, aniso linear 3 + let lin_of = |lw: &mut Lw, f: Value| { + let x = lw.lshrc(f, 1); + let x = lw.or(f, x); + lw.andc(x, 1) + }; + let mag_l = lin_of(self, mag); + let min_l = lin_of(self, min); + let same = self.cmp(Pred::Eq, mag_l, min_l); + let exact = self.lor(up, same); + let iso = self.lnot(aniso); + let pm = self.land(linear, iso); + let pm = self.land(pm, exact); + let frac = self.fsub(l, l0); + let mut r = s1; + for (i, v) in r.iter_mut().enumerate() { + let dlt = self.fsub(m1[i], m0[i]); + let x = self.fmul(frac, dlt); + let x = self.fadd(m0[i], x); + *v = self.sel(pm, x, s1[i]); + } + r + } _ if has_lz || !ps => { let z = self.fc(0.0); let l = floor(self, z); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index e1f73ef484..14fa0b91c4 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2061,7 +2061,7 @@ fn gpu_ps_image_sample_offset_implicit_lod_between_mips() { return; } use windows::Win32::Graphics::Direct3D12::*; - use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_SAMPLE_DESC}; + use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_SAMPLE_DESC}; let scale = 2f32.powf(0.25) / 8.0; let ps = prog(&[ ("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 129)], None), // offset x +1, y 0 @@ -2102,14 +2102,29 @@ fn gpu_ps_image_sample_offset_implicit_lod_between_mips() { let mut sgprs = vec![0u32; 32]; sgprs[16..24].copy_from_slice(&tsharp(0, 77, 9)); sgprs[24..28].copy_from_slice(&s_sharp); - let px = draw_console_views(None, Some(&ps), &[[0.0, 0.0], [0.0, 32.0], [32.0, 0.0]], &sgprs, &[tex], &[crate::gpu::desc::sampler_d3d12(&s_sharp, false)]); - for y in 0..16 { - for x in 0..16 { - let u = (x as f32 + 0.5) * scale; - let texel = |size: f32| ((u * size).floor() + 1.0).clamp(0.0, size - 1.0); - let want = 0.75 * texel(8.0) + 0.25 * (100.0 + texel(4.0)); - let got = px[y * 16 + x][0]; - assert!((got - want).abs() < 0.02, "pixel ({x},{y}) = {got}, expected {want}"); + let tris = [[0.0, 0.0], [0.0, 32.0], [32.0, 0.0]]; + let samplers = [crate::gpu::desc::sampler_d3d12(&s_sharp, false)]; + let hlsl = draw_console_views(None, Some(&ps), &tris, &sgprs, std::slice::from_ref(&tex), &samplers); + // the same pixel shader through the direct DXIL translator (HLSL pass-through VS) + let vs = translate(&draw_passthrough_vs(), &runner::options(Mode::Vertex(draw_vsi()))).unwrap(); + let vsb = super::dxc::compile(&vs.hlsl, "main", "vs_6_6", &[]).unwrap(); + let ko = crate::gpu::dxil::kernel::KernelOptions { wave64: runner::options(Mode::Pixel(draw_psi())).wave64, split: false, mem_windows: 1, sampler_base: 0 }; + let dps = crate::gpu::dxil::kernel::translate_pixel(&ps, ko, &draw_psi()).unwrap_or_else(|e| panic!("direct DXIL: {e}")).dxil; + let verts: Vec = tris.iter().flat_map(|p| [p[0] / 8.0 - 1.0, 1.0 - p[1] / 8.0, 0.0, 0.0, 0.0, 0.0]).collect(); + let layout = [ + runner::VertexElement { semantic: "POSITION", index: 0, format: DXGI_FORMAT_R32G32_FLOAT, offset: 0 }, + runner::VertexElement { semantic: "COLOR", index: 0, format: DXGI_FORMAT_R32G32B32A32_FLOAT, offset: 8 }, + ]; + let direct = runner::draw_views(&vsb, &dps, &layout, &verts, 24, &sgprs, &[], std::slice::from_ref(&tex), &samplers, 16, [-1.0; 4]).unwrap(); + for (backend, px) in [("HLSL", &hlsl), ("direct", &direct)] { + for y in 0..16 { + for x in 0..16 { + let u = (x as f32 + 0.5) * scale; + let texel = |size: f32| ((u * size).floor() + 1.0).clamp(0.0, size - 1.0); + let want = 0.75 * texel(8.0) + 0.25 * (100.0 + texel(4.0)); + let got = px[y * 16 + x][0]; + assert!((got - want).abs() < 0.02, "{backend}: pixel ({x},{y}) = {got}, expected {want}"); + } } } }