Skip to content
Closed
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
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 18 additions & 5 deletions crates/xcl/src/gpu/dxil/lower_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,18 +613,31 @@ 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: 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);
Expand Down
28 changes: 24 additions & 4 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ pub fn draw_gs(
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, 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`).
Expand All @@ -722,7 +722,7 @@ pub fn draw_gs_indexed(
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, 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`).
Expand All @@ -740,7 +740,26 @@ pub fn draw_views(
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, 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<Vec<[f32; 4]>, String> {
draw_gs_views(vs, None, ps, layout, vertices, stride, &[], sgprs, mem, textures, samplers, static_samplers, size, clear)
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -756,14 +775,15 @@ fn draw_gs_views(
mem: &[u32],
textures: &[ViewTexture],
samplers: &[D3D12_SAMPLER_DESC],
static_samplers: &[super::pso::StaticSampler],
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, String> {
let mut g = GPU.lock().unwrap();
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<std::ffi::CString> = layout.iter().map(|l| std::ffi::CString::new(l.semantic).unwrap()).collect();
let elems: Vec<D3D12_INPUT_ELEMENT_DESC> = layout
.iter()
Expand Down
93 changes: 93 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32> = [[-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() {
Expand Down
Loading