Skip to content
Open
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
30 changes: 21 additions & 9 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ fn sample_coord(dim: u64, c: &[String]) -> String {
}
}

/// Spatial-only MIMG coordinates per DIM for HLSL `CalculateLevelOfDetail(Unclamped)`
/// (array layers are omitted; cube arrays take the 3D direction only).
fn lod_coord(dim: u64, c: &[String]) -> String {
match dim {
0 | 4 => c[0].clone(),
1 | 5 => format!("float2({}, {})", c[0], c[1]),
2 => format!("float3({}, {}, {})", c[0], c[1], c[2]),
3 => format!("cube_dir({}, {}, {})", c[0], c[1], c[2]),
_ => String::new(),
}
}

/// The coordinates moved by the offset vector `o` (from `texel_offset`); array layers stay.
fn moved_coords(dim: u64, c: &[String], o: &str) -> Vec<String> {
c.iter().enumerate().map(|(k, x)| if (dim == 4 && k == 1) || (dim == 5 && k == 2) { x.clone() } else { format!("({x} + {o}[{k}])") }).collect()
Expand Down Expand Up @@ -2758,7 +2770,7 @@ impl Em {
} else {
None
};
let coord0 = sample_coord(dim, &coords);
let coord0 = lod_coord(dim, &coords);
let coords_unmoved = coords.clone();
let coords: Vec<String> = if offset.is_some() { moved_coords(dim, &coords, &format!("{t}_o")) } else { coords };
let (ty, coord) = match dim {
Expand Down Expand Up @@ -2882,16 +2894,16 @@ impl Em {
return Err("image_get_lod outside PS");
}
let coords: Vec<String> = (0..body as usize).map(|k| compf(self, i_body + k)).collect();
let (ty, coord) = match dim {
0 => ("Texture1D<float4>", coords[0].clone()),
1 => ("Texture2D<float4>", format!("float2({}, {})", coords[0], coords[1])),
2 => ("Texture3D<float4>", format!("float3({}, {}, {})", coords[0], coords[1], coords[2])),
// a cube array's level of detail takes the direction only
3 => ("TextureCubeArray<float4>", format!("cube_dir({}, {}, {})", coords[0], coords[1], coords[2])),
4 => ("Texture1DArray<float4>", format!("float2({}, round({}))", coords[0], coords[1])),
5 => ("Texture2DArray<float4>", format!("float3({}, {}, round({}))", coords[0], coords[1], coords[2])),
let ty = match dim {
0 => "Texture1D<float4>",
1 => "Texture2D<float4>",
2 => "Texture3D<float4>",
3 => "TextureCubeArray<float4>",
4 => "Texture1DArray<float4>",
5 => "Texture2DArray<float4>",
_ => return Err("MIMG dim"),
};
let coord = lod_coord(dim, &coords);
let s3 = self.sreg(ssamp + 3);
self.line(&format!("uint {t}_tw0 = select({t}_ok, {tw0}, 0u);"));
self.line(&format!("{ty} {t}_T = ResourceDescriptorHeap[NonUniformResourceIndex({t}_tw0)];"));
Expand Down
151 changes: 151 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,157 @@ fn implicit_lod_between_mips(s_sharp: [u32; 4]) {
}
}

/// HLSL `CalculateLevelOfDetail(Unclamped)` on `Texture1DArray` (DIM 4) and `Texture2DArray`
/// (DIM 5) takes spatial-only coordinates (`float` / `float2`, omitting the array layer), for
/// both `image_get_lod` and implicit-LOD `image_sample_o`.
#[test]
fn ps_array_texture_calculate_lod_uses_spatial_coords() {
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_SAMPLE_DESC};
let scale = 2f32.powf(0.25) / 8.0;
let ps_get_lod_1da = prog(&[
("V_MUL_F32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 255), ("VSRC1", 2)], Some(scale.to_bits())),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 128)], None), // slice 0.0
("IMAGE_GET_LOD", "ENC_MIMG", &[("VDATA", 8), ("VADDR", 4), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x3), ("DIM", 4)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 9), ("VSRC2", 8), ("VSRC3", 9), ("DONE", 1), ("VM", 1)], None),
]);
let ps_get_lod_2da = prog(&[
("V_MUL_F32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 255), ("VSRC1", 2)], Some(scale.to_bits())),
("V_MUL_F32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 255), ("VSRC1", 3)], Some(scale.to_bits())),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", 128)], None), // slice 0.0
("IMAGE_GET_LOD", "ENC_MIMG", &[("VDATA", 8), ("VADDR", 4), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x3), ("DIM", 5)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 9), ("VSRC2", 8), ("VSRC3", 9), ("DONE", 1), ("VM", 1)], None),
]);
let ps_sample_o_1da = prog(&[
("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 129)], None), // offset x +1
("V_MUL_F32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 255), ("VSRC1", 2)], Some(scale.to_bits())),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", 128)], None), // slice 0.0
("IMAGE_SAMPLE_O", "ENC_MIMG", &[("VDATA", 8), ("VADDR", 4), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x1), ("DIM", 4)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 8), ("VSRC2", 8), ("VSRC3", 8), ("DONE", 1), ("VM", 1)], None),
]);
let ps_sample_o_2da = prog(&[
("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 129)], None), // offset x +1, y 0
("V_MUL_F32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 255), ("VSRC1", 2)], Some(scale.to_bits())),
("V_MUL_F32", "ENC_VOP2", &[("VDST", 6), ("SRC0", 255), ("VSRC1", 3)], Some(scale.to_bits())),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 7), ("SRC0", 128)], None), // slice 0.0
("IMAGE_SAMPLE_O", "ENC_MIMG", &[("VDATA", 8), ("VADDR", 4), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x1), ("DIM", 5)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 8), ("VSRC2", 8), ("VSRC3", 8), ("DONE", 1), ("VM", 1)], None),
]);

let has_dxc = super::dxc::location().is_ok();
for (label, code, bad_needle) in [
("get_lod 1DArray", &ps_get_lod_1da, "CalculateLevelOfDetail(t0_S, float2("),
("get_lod 2DArray", &ps_get_lod_2da, "CalculateLevelOfDetail(t0_S, float3("),
("sample_o 1DArray", &ps_sample_o_1da, "CalculateLevelOfDetail(t0_S, float2("),
("sample_o 2DArray", &ps_sample_o_2da, "CalculateLevelOfDetail(t0_S, float3("),
] {
let t = translate(code, &Options { mode: Mode::Pixel(draw_psi()), ..Options::default() }).expect(label);
assert!(t.complete(), "{label}: {:?}", t.unsupported_why);
assert!(!t.hlsl.contains(bad_needle), "{label} passed array slice to CalculateLevelOfDetail:\n{}", t.hlsl);
if has_dxc {
super::dxc::compile(&t.hlsl, "main", "ps_6_6", &[]).unwrap_or_else(|e| panic!("{label} DXC compile failed: {e}\n{}", t.hlsl));
}
}

if !gpu_ready() {
return;
}

let s_sharp = [2 | 2 << 3 | 2 << 6, 0xFFF << 12, 2 << 26, 0];
let sampler = crate::gpu::desc::sampler_d3d12(&s_sharp, false);
let tri = [[0.0, 0.0], [0.0, 32.0], [32.0, 0.0]];

// 4-level Texture1DArray (8, 4, 2, 1)
let mut data1 = Vec::new();
for k in 0..4u32 {
let n = 8 >> k;
for x in 0..n {
data1.extend_from_slice(&[(100 * k + x) as f32; 4].map(f32::to_le_bytes).concat());
}
}
let res1 = D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE1D,
Width: 8,
Height: 1,
DepthOrArraySize: 1,
MipLevels: 4,
Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
..Default::default()
};
let mut srv1 = D3D12_SHADER_RESOURCE_VIEW_DESC {
Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
ViewDimension: D3D12_SRV_DIMENSION_TEXTURE1DARRAY,
Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
..Default::default()
};
srv1.Anonymous.Texture1DArray = D3D12_TEX1D_ARRAY_SRV { MipLevels: 4, ArraySize: 1, ..Default::default() };
let mut sgprs1 = vec![0u32; 32];
sgprs1[16..24].copy_from_slice(&tsharp(0, 77, 12));
sgprs1[24..28].copy_from_slice(&s_sharp);

// 4-level Texture2DArray (8x8, 4x4, 2x2, 1x1)
let mut data2 = Vec::new();
for k in 0..4u32 {
let n = 8 >> k;
for _y in 0..n {
for x in 0..n {
data2.extend_from_slice(&[(100 * k + x) as f32; 4].map(f32::to_le_bytes).concat());
}
}
}
let res2 = D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Width: 8,
Height: 8,
DepthOrArraySize: 1,
MipLevels: 4,
Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
..Default::default()
};
let mut srv2 = D3D12_SHADER_RESOURCE_VIEW_DESC {
Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2DARRAY,
Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
..Default::default()
};
srv2.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_SRV { MipLevels: 4, ArraySize: 1, ..Default::default() };
let mut sgprs2 = vec![0u32; 32];
sgprs2[16..24].copy_from_slice(&tsharp(0, 77, 13));
sgprs2[24..28].copy_from_slice(&s_sharp);

for (label, ps, tex, sgprs) in [
("get_lod 1DArray", &ps_get_lod_1da, runner::ViewTexture { res: res1, srv: srv1, data: data1.clone(), row_bytes: 8 * 16 }, &sgprs1),
("get_lod 2DArray", &ps_get_lod_2da, runner::ViewTexture { res: res2, srv: srv2, data: data2.clone(), row_bytes: 8 * 16 }, &sgprs2),
] {
let px = draw_console_views(None, Some(ps), &tri, sgprs, &[tex], &[sampler]);
for y in 0..16 {
for x in 0..16 {
let [clamped, unclamped, _, _] = px[y * 16 + x];
assert!((clamped - 0.25).abs() < 0.03, "{label} ({x},{y}) clamped={clamped}");
assert!((unclamped - 0.25).abs() < 0.03, "{label} ({x},{y}) unclamped={unclamped}");
}
}
}

for (label, ps, tex, sgprs) in [
("sample_o 1DArray", &ps_sample_o_1da, runner::ViewTexture { res: res1, srv: srv1, data: data1, row_bytes: 8 * 16 }, &sgprs1),
("sample_o 2DArray", &ps_sample_o_2da, runner::ViewTexture { res: res2, srv: srv2, data: data2, row_bytes: 8 * 16 }, &sgprs2),
] {
let px = draw_console_views(None, Some(ps), &tri, sgprs, &[tex], &[sampler]);
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, "{label} ({x},{y}) = {got}, expected {want}");
}
}
}
}

/// 48 SGPR spill slots in lanes 0..47 of v30 (`v_writelane` / `v_readlane`, as 287 retail
/// pixel shaders do – 11 up to lane 47): slot k = `slot(k)`, then s44 = the sum of slots
/// 0..31, s45 = the sum of slots 32..47.
Expand Down
Loading