From cce55296c854bebcc283bbdb95c6e61a23791d00 Mon Sep 17 00:00:00 2001 From: "Yohann F." <48117641+Flyander@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:19:57 +0100 Subject: [PATCH] Shaders: support static samplers in image_get_lod --- crates/xcl/src/gpu/dxil/kernel.rs | 2 +- crates/xcl/src/gpu/dxil/lower_image.rs | 23 +++++-- crates/xcl/src/gpu/recomp/runner.rs | 28 ++++++-- crates/xcl/src/gpu/recomp/tests.rs | 93 ++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 10 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index efcf15a00c..87ced892a9 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -36,7 +36,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 = 5; +pub const TRANSLATOR_VERSION: u32 = 6; /// `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 dc37a95569..ce5fa59547 100644 --- a/crates/xcl/src/gpu/dxil/lower_image.rs +++ b/crates/xcl/src/gpu/dxil/lower_image.rs @@ -613,9 +613,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(), @@ -623,8 +620,24 @@ 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: Sampler = match self.static_samplers.get(&d.pc).copied() { + Some(w) => match self.static_used.iter().find(|u| u.0 == w && !u.1) { + 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, false); + self.s.m.set_block(cur); + self.static_used.push((w, false, h)); + h + } + }, + None => { + let si = self.andc(s3, 0xFFF); + self.s.heap_sampler(si, false, true) + } + }; 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); diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs index f647dbd2c6..3a6b401bd7 100644 --- a/crates/xcl/src/gpu/recomp/runner.rs +++ b/crates/xcl/src/gpu/recomp/runner.rs @@ -706,7 +706,7 @@ pub fn draw_gs( size: u32, clear: [f32; 4], ) -> Result, String> { - draw_gs_views(vs, gs, ps, layout, vertices, stride, &[], sgprs, mem, &[], &[], size, clear) + draw_gs_views(vs, gs, ps, layout, vertices, stride, &[], sgprs, mem, &[], &[], &[], size, clear) } /// `draw_gs` of an indexed triangle list (`indices`). @@ -722,7 +722,7 @@ pub fn draw_gs_indexed( size: u32, clear: [f32; 4], ) -> Result, String> { - draw_gs_views(vs, gs, ps, layout, vertices, stride, indices, &[], &[], &[], &[], size, clear) + draw_gs_views(vs, gs, ps, layout, vertices, stride, indices, &[], &[], &[], &[], &[], size, clear) } /// `draw` with textures and samplers in the directly indexed heaps (see `ViewTexture`). @@ -740,7 +740,26 @@ pub fn draw_views( size: u32, clear: [f32; 4], ) -> Result, String> { - draw_gs_views(vs, None, ps, layout, vertices, stride, &[], sgprs, mem, textures, samplers, size, clear) + draw_gs_views(vs, None, ps, layout, vertices, stride, &[], sgprs, mem, textures, samplers, &[], size, clear) +} + +/// `draw_views` with static samplers on the graphics root signature. +#[allow(clippy::too_many_arguments)] +pub fn draw_views_with_static_samplers( + vs: &[u8], + ps: &[u8], + layout: &[VertexElement], + vertices: &[f32], + stride: u32, + sgprs: &[u32], + mem: &[u32], + textures: &[ViewTexture], + samplers: &[D3D12_SAMPLER_DESC], + static_samplers: &[super::pso::StaticSampler], + size: u32, + clear: [f32; 4], +) -> Result, String> { + draw_gs_views(vs, None, ps, layout, vertices, stride, &[], sgprs, mem, textures, samplers, static_samplers, size, clear) } #[allow(clippy::too_many_arguments)] @@ -756,6 +775,7 @@ fn draw_gs_views( mem: &[u32], textures: &[ViewTexture], samplers: &[D3D12_SAMPLER_DESC], + static_samplers: &[super::pso::StaticSampler], size: u32, clear: [f32; 4], ) -> Result, String> { @@ -763,7 +783,7 @@ fn draw_gs_views( let gpu = g.get_or_insert_with(init).as_mut().map_err(|x| x.clone())?; unsafe { let dev = &gpu.dev; - let rs = root_signature(dev, true)?; + let rs = if static_samplers.is_empty() { root_signature(dev, true)? } else { super::pso::abi_root_signature_with(dev, true, static_samplers)? }; let names: Vec = layout.iter().map(|l| std::ffi::CString::new(l.semantic).unwrap()).collect(); let elems: Vec = layout .iter() diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 846cb5cd86..c03c6105a6 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2505,6 +2505,99 @@ fn gpu_static_samplers_from_constants_in_code() { } } +/// `image_get_lod` in the direct pixel translator accepts a static S# and uses its root-signature sampler. +#[test] +fn gpu_direct_image_get_lod_static_sampler() { + if runner::adapter().is_err() { + runner::skip_test("no GPU"); + return; + } + if super::dxc::location().is_err() { + runner::skip_test("no DXC"); + return; + } + use windows::Win32::Graphics::Direct3D12::*; + use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_SAMPLE_DESC}; + + let clamp = 2 | 2 << 3 | 2 << 6; + let point = [clamp, 0x00FF_F000u32, 0, 0]; + let mut code = Vec::new(); + for (k, word) in point.iter().enumerate() { + code.extend(if *word == 0 { + ins("S_MOV_B32", "ENC_SOP1", &[("SDST", 24 + k as u64), ("SSRC0", 128)], None) + } else { + ins("S_MOV_B32", "ENC_SOP1", &[("SDST", 24 + k as u64), ("SSRC0", 255)], Some(*word)) + }); + } + let scale = 0.25f32 / 8.0; + code.extend(ins("V_MUL_F32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 255), ("VSRC1", 2)], Some(scale.to_bits()))); + code.extend(ins("V_MUL_F32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 255), ("VSRC1", 3)], Some(scale.to_bits()))); + code.extend(ins("IMAGE_GET_LOD", "ENC_MIMG", &[("VDATA", 8), ("VADDR", 4), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x3), ("DIM", 1)], None)); + code.extend(ins("V_MOV_B32", "ENC_VOP1", &[("VDST", 10), ("SRC0", 128)], None)); + code.extend(ins("V_MOV_B32", "ENC_VOP1", &[("VDST", 11), ("SRC0", 128)], None)); + code.extend(ins("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 9), ("VSRC2", 10), ("VSRC3", 11), ("DONE", 1), ("VM", 1)], None)); + code.extend(ins("S_ENDPGM", "ENC_SOPP", &[], None)); + + let psi = draw_psi(); + let ps = crate::gpu::dxil::kernel::translate_pixel( + &code, + crate::gpu::dxil::kernel::KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }, + &psi, + ) + .unwrap(); + assert_eq!(ps.static_samplers, vec![(point, false)]); + + let vs = translate(&draw_passthrough_vs(), &runner::options(Mode::Vertex(draw_vsi()))).unwrap(); + assert!(vs.complete(), "{:?}", vs.unsupported_why); + let vs = super::dxc::compile(&vs.hlsl, "main", "vs_6_6", &[]).unwrap(); + let res = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 8, + Height: 8, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_R32G32B32A32_FLOAT, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + let mut srv = D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: DXGI_FORMAT_R32G32B32A32_FLOAT, + ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D, + Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + ..Default::default() + }; + srv.Anonymous.Texture2D = D3D12_TEX2D_SRV { MipLevels: 1, ..Default::default() }; + let texture = runner::ViewTexture { res, srv, data: vec![0; 8 * 8 * 16], row_bytes: 8 * 16 }; + let vertices: Vec = [[-1.0f32, -1.0], [-1.0, 3.0], [3.0, -1.0]] + .iter() + .flat_map(|p| [p[0], p[1], 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 mut sgprs = vec![0; 32]; + sgprs[16..24].copy_from_slice(&tsharp(0, 77, 9)); + let pixels = runner::draw_views_with_static_samplers( + &vs, + &ps.dxil, + &layout, + &vertices, + 24, + &sgprs, + &[], + &[texture], + &[], + &ps.static_samplers, + 16, + [-1.0; 4], + ) + .unwrap(); + for (i, pixel) in pixels.iter().enumerate() { + assert!((pixel[0] - 0.0).abs() < 0.02 && (pixel[1] + 2.0).abs() < 0.02, "pixel {i}: {pixel:?}"); + } +} + #[test] fn gpu_images_a16_d16() { if !gpu_ready() {