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
46 changes: 37 additions & 9 deletions crates/victory-shader/src/emit/diff_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ V vs(float4 p : POSITION, float2 uv : TEXCOORD0) {
}
"#;

const SIGNED_OFFSETS: &str = "signed sample offsets";

/// Pixel shaders, each `ps`.
const CASES: &[(&str, &str)] = &[
(
Expand All @@ -58,6 +60,15 @@ const CASES: &[(&str, &str)] = &[
return r;
}"#,
),
(
SIGNED_OFFSETS,
r#"[RootSignature(RS)] float4 ps(V i) : SV_Target {
float2 center = float2(0.5, 0.5);
return float4(tex.Sample(smp, center, int2(-8, 7)).z,
tex.Sample(smp, center, int2(7, -8)).z,
tex.Sample(smp, center).z, 1);
}"#,
),
// A wave-wide range scalarises a per-pixel loop (as the title's light
// lists do): the console's 64-lane sections become host wave operations;
// the result is the same for any wave the pixel lands in.
Expand Down Expand Up @@ -762,21 +773,31 @@ fn translated_shaders_draw_as_pc_ones() {
3.0, -1.0, 0.5, 1.0, 2.0, 1.0,
];
let textures: [(u32, u32, &[[f32; 4]]); 2] = [(1, 1, &dummy), (4, 4, &texels)];
let draw = |roots| Draw {
roots,
textures: &textures,
samplers: &samplers,
vertices: &vertices,
stride: 24,
size: (16, 16),
mesh_groups: 0,
};
// Odd dimensions place UV (0.5, 0.5) at the center of texel (16, 16).
// The single mip, point sampler and interior offsets fix each sample.
let offset_texels: Vec<[f32; 4]> = (0..33 * 33).map(|i| [0.0, 0.0, i as f32, 1.0]).collect();
let offset_textures = [(1, 1, &dummy[..]), (33, 33, &offset_texels[..])];
let offset_samplers = [samplers[0]; 2];
let mut failures = Vec::new();
let only = std::env::var("VICTORY_DIFF_ONLY").ok();
for (name, src) in CASES
.iter()
.filter(|c| only.as_ref().is_none_or(|o| o == c.0))
{
let (textures, samplers) = if *name == SIGNED_OFFSETS {
(&offset_textures, &offset_samplers)
} else {
(&textures, &samplers)
};
let draw = |roots| Draw {
roots,
textures,
samplers,
vertices: &vertices,
stride: 24,
size: (16, 16),
mesh_groups: 0,
};
// A case may bring its own vertex shader.
let vs = if src.contains(" vs16(") { "vs16" } else { "vs" };
let vs_c = translate(&compile(&console, &dir, src, vs, "vs_6_5")).unwrap();
Expand Down Expand Up @@ -848,6 +869,13 @@ fn translated_shaders_draw_as_pc_ones() {
)
.unwrap();

if *name == SIGNED_OFFSETS {
let texel = |x, y| (y * 33 + x) as f32;
let expected = [texel(8, 23), texel(23, 8), texel(16, 16), 1.0];
for (backend, pixels) in [("translated", &ours), ("native", &theirs)] {
assert_eq!(pixels[8 * 16 + 8], expected, "{backend} sample offsets");
}
}
let worst = ours
.iter()
.flatten()
Expand Down
105 changes: 105 additions & 0 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6617,6 +6617,111 @@ mod tests {
.map(|_| ())
}

#[test]
fn image_sample_o_checks_signed_offsets_on_both_texture_axes() {
use super::super::container::SigElement;
use crate::{ResourceType, RootParam, RootType, UsedResource};

let compile = |packed| {
let words = [
0xBE89_0380, // s_mov_b32 s9, 0
0xBE8B_0380, // s_mov_b32 s11, 0
0xF40C_0004,
0xFA00_0000, // s_load_dwordx8 s[0:7], s[8:9], 0
0xF408_0305,
0xFA00_0000, // s_load_dwordx4 s[12:15], s[10:11], 0
0xBF8C_C07F, // s_waitcnt lgkmcnt(0)
0x7E00_02FF,
packed, // v_mov_b32 v0, packed
0x7E02_02F0,
0x7E04_02F0, // v_mov_b32 v1/v2, 0.5
0xF0C0_0F0A,
0x0060_0400,
0x0000_0201, // image_sample_o 2D
0xBF8C_3F70, // s_waitcnt vmcnt(0)
0xF800_180F,
0x0706_0504, // exp mrt0, v4-v7, done
S_ENDPGM,
];
let table = |user_data, slot| RootParam {
root: RootType::Table,
table_offset_dwords: 0,
sgprs: 1,
base_register: 0,
user_data,
slot,
};
let binding = |kind, user_data, slot, dwords| UsedResource {
kind,
root: RootType::Table,
table_offset_dwords: 0,
lower_bound: 0,
stride: 0,
dwords,
user_data,
slot,
};
let native = NativeProgram {
stage: HwStage::Ps,
wave64: false,
vgprs: 8,
layout: vec![table(8, 0), table(10, 1)],
used: vec![
binding(ResourceType::Srv, 8, 0, 8),
binding(ResourceType::Sampler, 10, 1, 4),
],
io: StageIo::Ps {
inputs: vec![],
input_addr: 0,
db_shader_control: 0,
patches: vec![],
},
code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
};
let p = ir::build(&native.code, false, 8).unwrap();
let res = resource::track(&p, &native.layout, native.stage)?;
let io = io::raise(&p, &native)?;
let st = structure::structure(&p)?;
lower(&Parts {
native: &native,
p: &p,
st: &st,
io: &io,
res: &res,
inputs: vec![],
outputs: vec![SigElement {
name: "SV_Target".into(),
index: 0,
kind: 16,
comp: 9,
interp: 0,
row: Some(0),
col: 0,
cols: 4,
usage: 0xf,
half: false,
}],
prims: vec![],
mesh_stride: 0,
})
};
compile(0x0738).unwrap(); // x = -8, y = 7.
compile(0x3807).unwrap(); // x = 7, y = -8.
// A six-bit field encodes -9 as 0x37. Check each axis independently.
for packed in [0x0008, 0x0037, 0x0800, 0x3700] {
assert!(
matches!(
compile(packed),
Err(crate::ShaderError::Unsupported {
what: "a sample offset past -8..7",
..
})
),
"accepted or misdiagnosed offset {packed:#06x}"
);
}
}

#[test]
fn a_scalar_carry_chain_reaches_the_select() {
// The high dword of a 64-bit add or subtract, then a select on the
Expand Down