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
33 changes: 33 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,3 +1568,36 @@ fn raw_buffer_oob_check_guards_32bit_wraparound() {
assert!(uge >= 4, "expected wraparound icmp uge checks for load and atomic (got {uge}):\n{dis}");
}
}

/// The console's quad sample mask (#142, `IO_QUAD_KILL`): a PS starts with the mask's test –
/// on the HLSL path the prologue `emit::quad_mask_prologue` right after the output's zeroing
/// (before EXEC is taken), with the io dwords 119 / 120 and one uniform branch; on the direct
/// path one discard more than the program's own (its DONE export's).
#[test]
fn quad_sample_mask_starts_pixel_shaders_on_both_paths() {
use crate::gpu::recomp::{dxc, emit::quad_mask_prologue, runner, translate as translate_hlsl, Mode, Options, PsInterface, IO_QUAD_KILL};
assert_eq!(IO_QUAD_KILL, [119, 120], "the io slots the host writes (`quad_mask::kill_words`)");
let p = quad_mask_prologue();
assert!(p.contains("io.Load(476u)") && p.contains("io.Load(480u)"), "{p}");
assert!(p.contains("[branch] if ((xcl_qk0 | xcl_qk1) != 0u)"), "one uniform test when nothing is masked:\n{p}");
assert!(p.contains("uint2(xi.xcl_pos.xy) & 1u") && p.contains("discard;"), "{p}");
let color = ("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 4), ("VSRC2", 4), ("VSRC3", 4), ("DONE", 1), ("VM", 1)][..], None);
let code = prog(&[("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 242)], None), color]);
let psi = PsInterface { inputs: vec![sig("SV_Position", 0, 1, 3, 0, 0xF)], outputs: vec![sig("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x2, attrs: None };
let t = translate_hlsl(&code, &Options { mode: Mode::Pixel(psi.clone()), wave64: true, ..Default::default() }).unwrap();
let start = format!("XclOut main(XclIn xi)\n{{\n XclOut xo = (XclOut)0;\n{p}");
assert!(t.hlsl.contains(&start), "HLSL\n{}", t.hlsl);
// the mask is tested before EXEC is taken
let main = &t.hlsl[t.hlsl.find("XclOut main(").unwrap()..];
assert!(main.find("xcl_qk0").unwrap() < main.find("ballot(true)").unwrap(), "{}", t.hlsl);
if dxc::location().is_err() {
return runner::skip_test("no DXC (the HLSL text is checked)");
}
let discards = |c: &[u8]| super::validate::disassemble(c).unwrap_or_else(|e| panic!("{e}")).matches("@dx.op.discard(i32 82").count();
let compiled = dxc::compile(&t.hlsl, "main", "ps_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl));
let ko = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 };
let direct = super::kernel::translate_pixel(&code, ko, &psi).unwrap_or_else(|e| panic!("{}", &e[..e.len().min(800)]));
// the program's own discard (lanes without a DONE export), and the mask's
assert_eq!(discards(&direct.dxil), 2, "direct path's discards");
assert!(discards(&compiled) >= 1, "HLSL path's discards");
}
30 changes: 30 additions & 0 deletions crates/xcl/src/gpu/dxil/lower_gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ impl Lw {
if let Some(v) = sys(14) {
vgpr.insert(v as u32, io.coverage(&mut self.s));
}
// the console's quad sample mask (#142), before EXEC is taken (a pixel shader given
// early depth must not get it: the depth of the pixels it discards would be written)
self.quad_mask_prologue(&mut io, pos);
self.gfx_common_init(vgpr);
for (o, _) in targets.values_mut() {
o.vars = self.out_vars(4);
Expand All @@ -251,6 +254,33 @@ impl Lw {
self.gfx = Some(GfxCtx { io, vs: None, ps: Some(ps.clone()), pos: None, params: Vec::new(), ps_in, targets, depth, ps_done: false, ps_vm: false, live_var, live_vm_var });
}

/// The console's quad sample mask (`IO_QUAD_KILL`; the HLSL path's
/// `emit::quad_mask_prologue`): the pixel at (x & 1, y & 1) of its quad is discarded when
/// the mask takes all 16 of its samples away. Straight-line: two loads of the same io
/// dwords for every lane and a few integer operations; a discard whose condition is false
/// in every draw the title does not mask (io slots 0).
fn quad_mask_prologue(&mut self, io: &mut GraphicsIo, pos: Sig) {
let Some(buf) = self.init.io else { return };
let [lo, hi] = crate::gpu::recomp::IO_QUAD_KILL;
let o = self.c(lo as u32 * 4);
let k0 = self.s.raw_load(buf, o, 1)[0];
let o = self.c(hi as u32 * 4);
let k1 = self.s.raw_load(buf, o, 1)[0];
let x = io.load_input(&mut self.s, pos, 0, 0);
let y = io.load_input(&mut self.s, pos, 0, 1);
let x = self.ftoui(x);
let y = self.ftoui(y);
let x = self.andc(x, 1);
let y = self.andc(y, 1);
let odd_row = self.nz(y);
let word = self.sel(odd_row, k1, k0);
let shift = self.shlc(x, 4);
let bits = self.lshr(word, shift);
let bits = self.andc(bits, 0xFFFF);
let dead = self.cmpc(Pred::Eq, bits, 0xFFFF);
io.discard(&mut self.s, dead);
}

/// SGPRs from `io`, SCC 0, EXEC = `ballot(true)`.
fn gfx_common_init(&mut self, vgpr: HashMap<u32, Value>) {
let zero = self.c(0);
Expand Down
19 changes: 19 additions & 0 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5526,6 +5526,10 @@ static uint xcl_cap = 1u << 16;
h.push_str(" float4 xcl_unused : SV_Target7;\n");
}
h.push_str("};\nXclOut main(XclIn xi)\n{\n XclOut xo = (XclOut)0;\n");
// the console's quad sample mask (#142). A pixel shader given early depth
// (`[earlydepthstencil]`) must not get it: the depth of the pixels it discards
// would still be written
h.push_str(&quad_mask_prologue());
if self.ps_writes_mask {
h.push_str(" xo.xcl_mask = 0xFFFFFFFFu;\n");
}
Expand Down Expand Up @@ -5578,6 +5582,21 @@ static uint xcl_cap = 1u << 16;
}
}

/// The start of every pixel shader: the console's quad sample mask
/// (`IO_QUAD_KILL`, command list #142). The pixel at (x & 1, y & 1) of its quad is discarded
/// when the mask takes all 16 of its samples away; nothing is done (one uniform branch) when it
/// takes none, which is every draw the title does not mask and every draw without
/// `XCL_SAMPLE_MASK=1`. Discarded before the program's EXEC is taken (`ballot(true)`), the
/// pixel is a helper lane for its quad's derivatives and out of EXEC, as a pixel the console's
/// mask leaves uncovered. (A mask that keeps some samples of a pixel keeps the whole pixel; the
/// retail title only sets 0 or 0xFFFF per pixel.)
pub fn quad_mask_prologue() -> String {
let [lo, hi] = super::IO_QUAD_KILL.map(|k| k * 4);
format!(
" uint xcl_qk0 = io.Load({lo}u);\n uint xcl_qk1 = io.Load({hi}u);\n [branch] if ((xcl_qk0 | xcl_qk1) != 0u) {{\n uint2 xcl_qp = uint2(xi.xcl_pos.xy) & 1u;\n if ((((xcl_qp.y != 0u ? xcl_qk1 : xcl_qk0) >> (xcl_qp.x << 4u)) & 0xFFFFu) == 0xFFFFu)\n discard;\n }}\n"
)
}

/// Vertex output struct shared by the VS mode and the mesh pass-through: SV_Position, then
/// the params in `exp param` order (non-system OSG1 elements, then clip/cull distances).
fn out_struct(h: &mut String, name: &str, outputs: &[SigElement]) {
Expand Down
7 changes: 7 additions & 0 deletions crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ const _: () = assert!(IO_THREAD_LIMIT[1] == IO_THREAD_LIMIT[0] + 1 && IO_THREAD_

/// First console mesh group in this dispatch slice. Record indices always start at zero.
pub const IO_MESH_GROUP_BASE: usize = 118;
/// io slots (trap-temp range, after `IO_MESH_GROUP_BASE`) of the console's quad sample mask
/// (command list #142 `SetQuadSampleMaskX(u64)`, the two PA_SC_AA_MASK registers: 16 sample
/// bits for each pixel of a 2×2 quad), stored inverted – the samples the mask takes away – so an
/// io block that does not write them (0) takes nothing away: [119] = pixels (x, y) = (0, 0) in
/// bits 0..15, (1, 0) in 16..31; [120] = (0, 1), (1, 1). A pixel shader discards a pixel all of
/// whose 16 bits are set (`emit::quad_mask_prologue`, `Lw::quad_mask_prologue`).
pub const IO_QUAD_KILL: [usize; 2] = [119, 120];

impl GsInterface {
/// bytes of one ES vertex in LDS (ESGS item: 16 per ES output register)
Expand Down
56 changes: 56 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,62 @@ fn gpu_ps_exp_vm_controls_pixel_survival() {
}
}

/// The console's quad sample mask (#142, `IO_QUAD_KILL`) on the GPU, through the HLSL and
/// the direct DXIL pixel shaders: each pixel (x, y) is drawn when the mask keeps the 16-bit
/// word of its quad position (x & 1) + 2 (y & 1), cleared otherwise – the retail title's
/// masks (all ones, both diagonals, 3 of 4, 1 of 4) and none.
#[test]
fn gpu_quad_sample_mask_discards_the_quad_pixels_it_takes_away() {
if !gpu_ready() {
return;
}
use super::{PsInterface, VsInterface, IO_QUAD_KILL, KERNEL_SGPRS};
use crate::gpu::dxil::kernel::{translate_pixel, translate_vertex, KernelOptions};
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;
let vs_code = prog(&[
("V_MOV_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", V + 4)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V + 5)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 128)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 242)], None),
("EXP", "ENC_EXP", &[("TGT", 12), ("EN", 0xF), ("VSRC0", 0), ("VSRC1", 1), ("VSRC2", 2), ("VSRC3", 3), ("DONE", 1)], None),
]);
let ps_code = prog(&[
("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 242)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 4), ("VSRC2", 4), ("VSRC3", 4), ("DONE", 1), ("VM", 1)], None),
]);
let vsi = VsInterface { inputs: vec![el("POSITION", 0, 0, 3, 0, 3)], outputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)], attr_base: 4, params: None };
let ko = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 };
let vo = Options { mode: Mode::Vertex(vsi.clone()), wave64: true, ..Default::default() };
let vs_hlsl = super::dxc::compile(&translate(&vs_code, &vo).unwrap().hlsl, "main", "vs_6_6", &[]).unwrap();
let vs_dxil = translate_vertex(&vs_code, ko, &vsi).unwrap().dxil;
let verts = [-1.0f32, -1.0, -1.0, 3.0, 3.0, -1.0];
let layout = [runner::VertexElement { semantic: "POSITION", index: 0, format: DXGI_FORMAT_R32G32_FLOAT, offset: 0 }];
let clear = [0.0f32, 0.0, 1.0, 1.0];
let masks = [u64::MAX, 0x0000_FFFF_FFFF_0000, 0xFFFF_0000_0000_FFFF, 0xFFFF_0000_FFFF_FFFF, 0x0000_FFFF_0000_0000, 0];
let psi = PsInterface { inputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)], outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None };
let po = Options { mode: Mode::Pixel(psi.clone()), wave64: true, ..Default::default() };
let pt = translate(&ps_code, &po).unwrap();
let ps_hlsl = super::dxc::compile(&pt.hlsl, "main", "ps_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", pt.hlsl));
let ps_dxil = translate_pixel(&ps_code, ko, &psi).unwrap().dxil;
for (backend, vs, ps) in [("hlsl", &vs_hlsl, &ps_hlsl), ("direct_dxil", &vs_dxil, &ps_dxil)] {
for mask in masks {
let kill = !mask;
let mut sgprs = vec![0u32; KERNEL_SGPRS];
sgprs[IO_QUAD_KILL[0]] = kill as u32;
sgprs[IO_QUAD_KILL[1]] = (kill >> 32) as u32;
let px = runner::draw(vs, ps, &layout, &verts, 8, &sgprs, &[], 16, clear).unwrap();
for y in 0..16usize {
for x in 0..16usize {
let word = (mask >> (16 * ((x & 1) + 2 * (y & 1)))) & 0xFFFF;
let want = if word != 0 { [1.0; 4] } else { clear };
let got = px[y * 16 + x];
assert!(got.iter().zip(want).all(|(a, b)| (a - b).abs() < 1e-5), "{backend}, mask {mask:#018x}: pixel ({x},{y}) = {got:?}, expected {want:?}");
}
}
}
}
}

/// `emit::rect_list_gs` declares the VS's output struct; the fourth vertex is v[0] with its
/// float outputs continued from the other three, its integer (flat) ones left as v[0]'s.
#[test]
Expand Down
Loading
Loading