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
56 changes: 55 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2749,7 +2749,13 @@ impl Em {
self.line(&format!("{ty} {t}_T = ResourceDescriptorHeap[NonUniformResourceIndex({idx})];"));
let sampler = self.sampler_ref(d.pc, has_c, &s3);
self.line(&format!("{samp} {t}_S = {sampler};"));
if let Some(o) = &offset {
// gather4_l / _l_o: the 2x2 footprint of the mip the S# and `lod` name. HLSL's Gather has no
// level, so the four texels are Loads (compare forms keep the mip-0 GatherCmp)
let glod = base.starts_with("gather4") && lod.is_some() && !has_c;
if glod && !matches!(dim, 1 | 5) {
return Err("gather4 explicit LOD (not 2D)");
}
if let (Some(o), false) = (&offset, glod) {
let level = match (&lod, has_lz || base.starts_with("gather4"), ps && grads.is_none() && !has_c) {
(_, true, _) => "0u".to_string(),
(Some(l), _, _) => format!("(uint)max(round({l}), 0.0f)"),
Expand All @@ -2764,6 +2770,47 @@ impl Em {
}
let ch = ["Red", "Green", "Blue", "Alpha"][comps[0] as usize];
match &cmp {
_ if glod => {
let l = lod.clone().unwrap_or_default();
let [w0, w1, w2, w3] = match self.static_ss.get(&d.pc) {
Some(w) => w.map(|x| format!("{x}u")),
None => [self.sreg(ssamp), self.sreg(ssamp + 1), self.sreg(ssamp + 2), s3.clone()],
};
let dims = |lv: &str| if dim == 5 { format!("{t}_T.GetDimensions({lv}, {t}_gw, {t}_gh, {t}_gz, {t}_gn);") } else { format!("{t}_T.GetDimensions({lv}, {t}_gw, {t}_gh, {t}_gn);") };
self.line(&format!("uint {t}_gw = 1u, {t}_gh = 1u, {t}_gz = 1u, {t}_gn = 1u; {}", dims("0u")));
// level (gfx1036 measurement): LOD + S# bias, clamped to the S# MIN_LOD..MAX_LOD, raised to the
// T# MIN_LOD, then the nearest level: the LOD is first rounded to 1/256 (halves up), then ties go up,
// both in floor(l + 257/512) (exact in f32); MIP_FILTER none reads floor(T# MIN_LOD). All u4.8.
// shortcut: T# MIN_LOD taken relative to the view's base level, measured with BASE_LEVEL 0 only
let tmin = format!("((float)(({tw1} >> 8) & 0xFFFu) / 256.0f)");
self.line(&format!(
"uint {t}_gl = min((({w2} >> 26) & 3u) == 0u ? (uint)floor({tmin}) : (uint)floor(max(clamp({l} + (float)(((int)({w2} << 18)) >> 18) / 256.0f, (float)({w1} & 0xFFFu) / 256.0f, (float)(({w1} >> 12) & 0xFFFu) / 256.0f), {tmin}) + 0.501953125f), max({t}_gn, 1u) - 1u); {}",
dims(&format!("{t}_gl"))
));
let o = offset.as_ref().map(|o| format!(" + int2(((int)({o} << 26)) >> 26, ((int)({o} << 18)) >> 26)")).unwrap_or_default();
self.line(&format!("int2 {t}_gs = int2((int){t}_gw, (int){t}_gh);"));
self.line(&format!("int2 {t}_gp = int2(floor(float2({}, {}) * float2({t}_gs) - 0.5f)){o};", coords_unmoved[0], coords_unmoved[1]));
// integer taps: the S# addressing is applied here, so no sampler LOD clamp / bias can change the level
self.line(&format!(
"int4 {t}_ga = int4(gat_ax({t}_gp.x, {t}_gs.x, {w0} & 7u), gat_ax({t}_gp.x + 1, {t}_gs.x, {w0} & 7u), gat_ax({t}_gp.y, {t}_gs.y, ({w0} >> 3) & 7u), gat_ax({t}_gp.y + 1, {t}_gs.y, ({w0} >> 3) & 7u));"
));
let ci = comps[0] as usize;
let border = if ci == 3 { format!("((({w3} >> 30) - 1u) < 2u ? 1.0f : 0.0f)") } else { format!("(({w3} >> 30) == 2u ? 1.0f : 0.0f)") };
let c = ["r", "g", "b", "a"][ci];
let layer = if dim == 5 {
self.line(&format!("int {t}_gk = min(max((int)round({}), 0), (int){t}_gz - 1);", coords_unmoved[2]));
format!(", {t}_gk")
} else {
String::new()
};
let ld = if dim == 5 { "int4" } else { "int3" };
// D3D Gather order: (x, y+1), (x+1, y+1), (x+1, y), (x, y)
let tap = |x: &str, y: &str| {
let (x, y) = (format!("{t}_ga.{x}"), format!("{t}_ga.{y}"));
format!("(({x} < 0 || {y} < 0) ? {border} : {t}_T.Load({ld}({x}, {y}{layer}, (int){t}_gl)).{c})")
};
format!("float4({}, {}, {}, {})", tap("x", "w"), tap("y", "w"), tap("y", "z"), tap("x", "z"))
}
Some(z) => format!("{t}_T.GatherCmp{ch}({t}_S, {coord}, {z})"),
None => format!("{t}_T.Gather{ch}({t}_S, {coord})"),
}
Expand Down Expand Up @@ -5934,6 +5981,13 @@ uint4 bvh_intersect(uint type, uint at, float tmax, float3 o, float3 d, float3 i
}
return uint4(0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu);
}
// gather4_l texel index i of an n-texel axis under S# address mode m (0 wrap, 1 mirror, 2 / 4 clamp, 3 / 5 mirror once, 6 clamp border,
// 7 mirror-once border; the half-border modes 4 / 5 give no border colour, as in desc::sampler_d3d12 and the direct backend); -1: the border colour
int gat_ax(int i, int n, uint m) {
uint t = i < 0 ? (uint)~i : (uint)i; uint r = t % (m == 1u ? 2u * (uint)n : (uint)n);
int v = m == 0u ? (i < 0 ? n - 1 - (int)r : (int)r) : m == 1u ? (r < (uint)n ? (int)r : 2 * n - 1 - (int)r) : (m & 1u) == 0u ? clamp(i, 0, n - 1) : (int)min(t, (uint)n - 1u);
return ((m == 6u && (i < 0 || i >= n)) || (m == 7u && t >= (uint)n)) ? -1 : v;
}
uint img_cls(uint fmt) { uint ty = select(fmt <= 77u, (FMT_INFO[min(fmt, 77u)] >> 3) & 7u, 6u); return select(ty == 4u, 1u, select(ty == 5u, 2u, 0u)); }
void put_bits(inout uint4 r, uint off, uint w, uint v) {
uint m = w >= 32u ? 0xFFFFFFFFu : ((1u << w) - 1u); v &= m;
Expand Down
46 changes: 36 additions & 10 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,34 @@ pub struct TestTexture {
/// array slices (cube: 6 per cube) or depth
pub depth: u32,
pub texel_bytes: u32,
/// tightly packed texels, slice after slice
/// tightly packed texels, slice after slice (each slice: its mip levels one after the other)
pub data: Vec<u8>,
pub uav: bool,
}

impl TestTexture {
/// Mip levels, from the data's length: an SRV-only 2D or 2D array texture whose `data` holds
/// every slice's full chain down to some level has that many; anything else has 1. (Not a
/// field, so the struct literals of tests that don't care need no change.)
pub fn mips(&self) -> u32 {
if self.uav || !matches!(self.dim, TexDim::D2 | TexDim::D2Array) {
return 1;
}
let mut bytes = 0usize;
for m in 0..16 {
let (w, h) = ((self.width >> m).max(1), (self.height >> m).max(1));
bytes += (w * h * self.texel_bytes * self.depth) as usize;
if bytes == self.data.len() {
return m + 1;
}
if bytes > self.data.len() || (w == 1 && h == 1) {
break;
}
}
1
}
}

/// A sampler for kernel tests at sampler heap index j.
pub struct TestSampler {
pub filter: D3D12_FILTER,
Expand Down Expand Up @@ -273,13 +296,13 @@ unsafe fn bind_resources(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, t
Width: t.width as u64,
Height: t.height,
DepthOrArraySize: depth_or_array as u16,
MipLevels: 1,
MipLevels: t.mips() as u16,
Format: t.format,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Flags: if t.uav { D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS } else { D3D12_RESOURCE_FLAG_NONE },
..Default::default()
} };
let subs = if t.dim == TexDim::D3 { 1 } else { t.depth };
let subs = if t.dim == TexDim::D3 { 1 } else { t.depth * t.mips() };
let mut fps = vec![D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); subs as usize];
let mut size = 0u64;
dev.GetCopyableFootprints(&rd, 0, subs, total, Some(fps.as_mut_ptr()), None, None, Some(&mut size));
Expand All @@ -298,17 +321,20 @@ unsafe fn bind_resources(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, t
mk += 1;
}
for (t, fps) in textures.iter().zip(&plans).filter(|(t, _)| t.dim.samples().is_none()) {
let row = (t.width * t.texel_bytes) as usize;
let slices = t.depth as usize;
let mut at = 0usize;
let slices = if t.dim == TexDim::D3 { t.depth } else { t.depth * t.mips() } as usize;
for z in 0..slices {
let (w, h) = if t.dim == TexDim::D3 { (t.width, t.height) } else { ((t.width >> (z as u32 % t.mips())).max(1), (t.height >> (z as u32 % t.mips())).max(1)) };
let row = (w * t.texel_bytes) as usize;
let fp = if t.dim == TexDim::D3 { &fps[0] } else { &fps[z] };
let slice_pitch = fp.Footprint.RowPitch as usize * t.height as usize;
let slice_pitch = fp.Footprint.RowPitch as usize * h as usize;
let zoff = if t.dim == TexDim::D3 { z * slice_pitch } else { 0 };
for y in 0..t.height as usize {
let src = &t.data[(z * t.height as usize + y) * row..][..row];
for y in 0..h as usize {
let src = &t.data[at + y * row..][..row];
let dst = (p as *mut u8).add(fp.Offset as usize + zoff + y * fp.Footprint.RowPitch as usize);
std::ptr::copy_nonoverlapping(src.as_ptr(), dst, row);
}
at += row * h as usize;
}
}
up.Unmap(0, None);
Expand Down Expand Up @@ -361,12 +387,12 @@ unsafe fn bind_resources(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, t
}
TexDim::D2 => {
sv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
sv.Anonymous.Texture2D = D3D12_TEX2D_SRV { MipLevels: 1, ..Default::default() };
sv.Anonymous.Texture2D = D3D12_TEX2D_SRV { MipLevels: t.mips(), ..Default::default() };
uv.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
}
TexDim::D2Array => {
sv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
sv.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_SRV { MipLevels: 1, ArraySize: t.depth, ..Default::default() };
sv.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_SRV { MipLevels: t.mips(), ArraySize: t.depth, ..Default::default() };
uv.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
uv.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_UAV { ArraySize: t.depth, ..Default::default() };
}
Expand Down
Loading
Loading