diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index 01a48a1b4..acb847aeb 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -133,6 +133,7 @@ fn direct_and_hlsl_graphics_stages_link() { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let (vo, po) = (Options { mode: Mode::Vertex(vsi.clone()), wave64: true, ..Default::default() }, Options { mode: Mode::Pixel(psi.clone()), wave64: true, ..Default::default() }); let hvs = crate::gpu::recomp::dxc::compile(&translate(&vs_code, &vo).unwrap().hlsl, "main", "vs_6_6", &[]).unwrap(); @@ -181,7 +182,7 @@ fn branchy_graphics_stages_validate() { } // v2 = front face (SPI_PS_INPUT_ENA bit 12 after the two I/J pairs of 0x3) let ps_code = body(("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 5), ("VSRC2", 2), ("VSRC3", 5), ("DONE", 1), ("VM", 1)], None)); - 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: 0x1002, attrs: None }; + 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: 0x1002, attrs: None, early_depth: false }; for wave64 in [true, false] { super::kernel::translate_pixel(&ps_code, KernelOptions { wave64, ..ko }, &psi).unwrap_or_else(|e| panic!("PS wave64 {wave64}: {}", &e[..e.len().min(800)])); } @@ -204,7 +205,7 @@ fn long_pixel_shader_translates_fast() { list.push(("EXP", "ENC_EXP", vec![("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 5), ("VSRC2", 6), ("VSRC3", 7), ("DONE", 1), ("VM", 1)], None)); let refs: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); let code = prog(&refs); - 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 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, early_depth: false }; let t = std::time::Instant::now(); let k = super::kernel::translate_pixel(&code, KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }, &psi).unwrap_or_else(|e| panic!("{}", &e[..e.len().min(800)])); println!("3000 instructions: {:?}, {} B", t.elapsed(), k.dxil.len()); @@ -254,6 +255,7 @@ fn graphics_stages_with_a_branch_before_their_exports() { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let ko = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }; let dvs = super::kernel::translate_vertex(&vs_code, ko, &vsi).unwrap_or_else(|e| panic!("VS: {}", &e[..e.len().min(600)])).dxil; @@ -429,7 +431,7 @@ fn ps_get_lod_with_a_static_sampler() { list.push(("EXP", "ENC_EXP", vec![("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 5), ("VSRC2", 6), ("VSRC3", 7), ("DONE", 1), ("VM", 1)], None)); let refs: Vec<(&str, &str, &[(&str, u64)], Option)> = list.iter().map(|(a, b, c, d)| (*a, *b, c.as_slice(), *d)).collect(); let code = prog(&refs); - 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 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, early_depth: false }; let ko = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }; let k = super::kernel::translate_pixel(&code, ko, &psi).unwrap_or_else(|e| panic!("{}", &e[..e.len().min(800)])); assert_eq!(k.static_samplers, vec![(linear, false)], "one static sampler, shared by get_lod and sample"); @@ -581,3 +583,48 @@ fn gpu_direct_unaligned_multi_dword_format_store() { let got: Vec = r.mem.iter().flat_map(|v| v.to_le_bytes()).collect(); assert_eq!(&got[0xFC..0x118], &want[0xFC..0x118], "bytes 0xFC..0x118 after the store at 0x102"); } + +/// A console PS with DB_SHADER_CONTROL.DEPTH_BEFORE_SHADER (`PsInterface::early_depth`) gets +/// `[earlydepthstencil]` on the HLSL path and the ForceEarlyDepthStencil shader flag (bit 3) on +/// both paths, also when it exports depth; without it, neither. +#[test] +fn early_depth_pixel_shaders_test_depth_first_on_both_paths() { + use crate::gpu::recomp::{dxc, runner, translate, Mode, Options, PsInterface}; + if dxc::location().is_err() { + return runner::skip_test("no DXC"); + } + // the shader flags of the entry (`!{i32 0, i64 , ...}`) + let early_flag = |c: &[u8]| { + let dis = super::validate::disassemble(c).unwrap_or_else(|e| panic!("{e}")); + dis.match_indices("!{i32 0, i64 ").any(|(i, m)| { + let n: String = dis[i + m.len()..].chars().take_while(char::is_ascii_digit).collect(); + n.parse::().is_ok_and(|f| f & 8 != 0) + }) + }; + let color = ("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 4), ("VSRC2", 4), ("VSRC3", 4), ("DONE", 1), ("VM", 1)][..], None); + let target = sig("SV_Target", 0, 64, 3, 0, 0xF); + let cases = [ + ("color", prog(&[("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 242)], None), color]), vec![target.clone()]), + ( + "color and depth", + prog(&[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 4), ("SRC0", 240)], None), + ("EXP", "ENC_EXP", &[("TGT", 8), ("EN", 0x1), ("VSRC0", 4), ("VSRC1", 4), ("VSRC2", 4), ("VSRC3", 4)], None), + color, + ]), + vec![target, sig("SV_Depth", 0, 65, 3, u32::MAX, 0x1)], + ), + ]; + let ko = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }; + for (name, code, outputs) in &cases { + for early_depth in [false, true] { + let psi = PsInterface { inputs: vec![sig("SV_Position", 0, 1, 3, 0, 0xF)], outputs: outputs.clone(), input_ena: 0x2, attrs: None, early_depth }; + let t = translate(code, &Options { mode: Mode::Pixel(psi.clone()), wave64: true, ..Default::default() }).unwrap(); + assert_eq!(t.hlsl.contains("[earlydepthstencil]\nXclOut main("), early_depth, "{name}, early {early_depth}: HLSL\n{}", t.hlsl); + let hlsl = dxc::compile(&t.hlsl, "main", "ps_6_6", &[]).unwrap_or_else(|e| panic!("{name}, early {early_depth}: {e}\n{}", t.hlsl)); + assert_eq!(early_flag(&hlsl), early_depth, "{name}, early {early_depth}: HLSL path's shader flags"); + let direct = super::kernel::translate_pixel(code, ko, &psi).unwrap_or_else(|e| panic!("{name}, early {early_depth}: {}", &e[..e.len().min(800)])); + assert_eq!(early_flag(&direct.dxil), early_depth, "{name}, early {early_depth}: direct path's shader flags"); + } + } +} diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 94ce0b37d..37cd3aa7c 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -561,7 +561,8 @@ impl Lw { let stage = self.s.stage; let c = if matches!(stage, Stage::Vertex | Stage::Pixel) { let g = self.gfx.take().ok_or("graphics entry without graphics I/O")?; - let info = if stage == Stage::Pixel { super::graphics::StageInfo::Pixel { early_depth_stencil: false } } else { super::graphics::StageInfo::Vertex }; + let early_depth_stencil = g.ps.as_ref().is_some_and(|p| p.early_depth); + let info = if stage == Stage::Pixel { super::graphics::StageInfo::Pixel { early_depth_stencil } } else { super::graphics::StageInfo::Vertex }; self.s.try_finish_graphics(g.io, info) } else { self.s.try_finish(numthreads, Some(hw)) diff --git a/crates/xcl/src/gpu/dxil/robustness_tests.rs b/crates/xcl/src/gpu/dxil/robustness_tests.rs index 46a06c63d..e9a2cedad 100644 --- a/crates/xcl/src/gpu/dxil/robustness_tests.rs +++ b/crates/xcl/src/gpu/dxil/robustness_tests.rs @@ -68,6 +68,7 @@ fn translate_all() -> Vec> { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 4, tgid: [true, false, false], tg_size: false, tid_components: 1 }; let o = |wave64, split| KernelOptions { wave64, split, mem_windows: 1, sampler_base: 0 }; diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043..a5b5239d6 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -5093,7 +5093,13 @@ impl Em { if !ps.outputs.iter().any(|e| matches!(e.system_value, 64 | 65 | 67 | 68)) { h.push_str(" float4 xcl_unused : SV_Target7;\n"); } - h.push_str("};\nXclOut main(XclIn xi)\n{\n XclOut xo = (XclOut)0;\n bool xcl_live = false;\n"); + h.push_str("};\n"); + // the console tests depth first: without it the PC runs (and keeps the memory + // writes of) hidden pixels too + if ps.early_depth { + h.push_str("[earlydepthstencil]\n"); + } + h.push_str("XclOut main(XclIn xi)\n{\n XclOut xo = (XclOut)0;\n bool xcl_live = false;\n"); h.push_str(if self.opt.wave64 { " lane_id = WaveGetLaneIndex();\n" } else { " uint xcl_hw_lane = WaveGetLaneIndex();\n xcl_w32 = WaveGetLaneCount() > 32u;\n xcl_half = xcl_w32 ? xcl_hw_lane >> 5 : 0u;\n lane_id = xcl_hw_lane & 31u;\n" }); if self.upper.is_some() { h.push_str(" xcl_hw32 = WaveGetLaneCount() < 64u;\n"); diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index d1e7d8bc4..f2cd3f120 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -151,6 +151,9 @@ pub struct PsInterface { pub input_ena: u32, /// the console's PS input table (`ps_input_table`; None: attributes in register order) pub attrs: Option>, + /// DB_SHADER_CONTROL.DEPTH_BEFORE_SHADER (`ps_shader_control`): depth and stencil are tested + /// before the program runs (`[earlydepthstencil]`), so it runs only for pixels that pass + pub early_depth: bool, } /// Mesh (NGG) shader interface. @@ -390,6 +393,17 @@ pub fn ps_input_table(fssm: &[u8]) -> Option<(u32, Vec)> { Some((ena, out)) } +/// DB_SHADER_CONTROL bit 12 (DEPTH_BEFORE_SHADER, as AMD's GFX10.3 register headers name it): +/// the compiler sets it for `[earlydepthstencil]`. +pub const DEPTH_BEFORE_SHADER: u32 = 1 << 12; + +/// DB_SHADER_CONTROL of an FSSM: the dword after SPI_PS_INPUT_ENA in the PS input table +/// (`ps_input_table`, +0x114; the same layout VIctory's `native.rs` reads it from). +pub fn ps_shader_control(fssm: &[u8]) -> Option { + let o = 0x130 + fssm_shift(fssm)? + 0x114; + fssm.get(o..o + 4).map(|b| u32::from_le_bytes(b.try_into().unwrap())) +} + /// The VS output table of an FSSM: per `exp param N`, the OSG1 register it writes. 32 records /// of 6 bytes at +0x1D4 (+ the sub-version shift): kind (6 user, 0 position), register, param /// number, 0, 0, 0; the record count after them (+0xC0). Usually param N = the N-th output @@ -469,7 +483,9 @@ pub fn interface(blob: &[u8], sh: &super::xbox::XboxShader, f: &super::xbox::Fss Some((ena, t)) => (ena, Some(t)), None => (f.data.get(0x248..0x24C).map(|b| u32::from_le_bytes(b.try_into().unwrap()))?, None), }; - Some(Mode::Pixel(PsInterface { inputs, outputs, input_ena: ena, attrs })) + // only where the input table was found: elsewhere the layout is not known + let early_depth = attrs.is_some() && ps_shader_control(&f.data).is_some_and(|c| c & DEPTH_BEFORE_SHADER != 0); + Some(Mode::Pixel(PsInterface { inputs, outputs, input_ena: ena, attrs, early_depth })) } // ES variant of a VS (before a GS) and the GS itself, each translatable on its own ("VS", 2) => Some(Mode::Geometry(GsInterface { es_inputs: inputs, es_outputs: outputs, max_vertex_count: 1, ..Default::default() })), @@ -1546,6 +1562,33 @@ mod attribute_tests { assert_eq!(ps_attribute_slot(&ps, 1, 0), Some(1)); } + /// DB_SHADER_CONTROL follows SPI_PS_INPUT_ENA in the input table: DEPTH_BEFORE_SHADER (bit + /// 12) makes a PS early-depth; other bits (Z export, kill, the ordered-section bit 16) do not. + #[test] + fn ps_depth_before_shader_from_the_input_table() { + let early = |sub: u32, control: u32, valid: bool| { + let base = if sub == 5 { 0x130 } else { 0x138 }; + let mut data = vec![0u8; 0x400]; + data[0x10..0x14].copy_from_slice(&sub.to_le_bytes()); + data[base..base + 8].copy_from_slice(&[6, 1, 0xF, 0, 0, 0, 1, 2]); + data[base + 0x100] = if valid { 1 } else { 33 }; + data[base + 0x110] = 2; + data[base + 0x114..base + 0x118].copy_from_slice(&control.to_le_bytes()); + assert_eq!(ps_shader_control(&data), Some(control)); + let f = super::super::xbox::Fssm { data, stages: vec![super::super::xbox::HwStage { offset: 0, hw: 5, api: 1 }], ..Default::default() }; + let sh = super::super::xbox::XboxShader { stage: "PS".into(), ..Default::default() }; + let Some(Mode::Pixel(ps)) = interface(&[], &sh, &f) else { panic!("not a PS") }; + ps.early_depth + }; + for sub in [5, 6] { + assert!(early(sub, DEPTH_BEFORE_SHADER, true), "sub-version {sub}"); + assert!(early(sub, DEPTH_BEFORE_SHADER | 1 << 16 | 0x41, true), "sub-version {sub}, with other bits"); + assert!(!early(sub, 1 << 16 | 0x41, true), "sub-version {sub}, without the bit"); + // no input table found: the layout is not known, the bit not trusted + assert!(!early(sub, DEPTH_BEFORE_SHADER, false), "sub-version {sub}, no table"); + } + } + /// Minecraft's UI vertex shader: an ISG1 input the program never reads (NORMAL0, r1) gets /// no fetch-shader VGPRs – POSITION0 (r2) is read in v8, TEXCOORD0 (r3) in v12. #[test] diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e64..e3f93dfb5 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1808,6 +1808,7 @@ fn gpu_graphics_vs_ps_draw() { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let (vo, po) = (Options { mode: Mode::Vertex(vsi), wave64: true, ..Default::default() }, Options { mode: Mode::Pixel(psi), wave64: true, ..Default::default() }); let vt = translate(&vs_code, &vo).unwrap(); @@ -1892,6 +1893,7 @@ fn ps_exp_vm_hlsl_and_direct_dxil_codegen() { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let po = Options { mode: Mode::Pixel(psi.clone()), wave64: true, ..Default::default() }; for (name, ps_code, has_vm) in ps_exp_vm_cases() { @@ -1941,6 +1943,7 @@ fn gpu_ps_exp_vm_controls_pixel_survival() { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let vo = Options { mode: Mode::Vertex(vsi.clone()), wave64: true, ..Default::default() }; let po = Options { mode: Mode::Pixel(psi.clone()), wave64: true, ..Default::default() }; @@ -2126,6 +2129,7 @@ fn gpu_rect_list_corners() { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let outputs = vsi.outputs.clone(); let vt = translate(&vs_code, &Options { mode: Mode::Vertex(vsi), wave64: true, ..Default::default() }).unwrap(); @@ -2196,6 +2200,7 @@ fn draw_psi() -> super::PsInterface { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, } } @@ -2699,7 +2704,7 @@ fn helper_lane_masks_ps(wave64: bool) -> super::Translation { ps_code.extend(encode("S_CBRANCH_EXECNZ", "ENC_SOPP", &[("SIMM16", back)], None).unwrap()); ps_code.extend(enc(&tail)); ps_code.extend(encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap()); - 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 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, early_depth: false }; translate(&ps_code, &Options { mode: Mode::Pixel(psi), wave64, ..Default::default() }).unwrap() } } @@ -6146,7 +6151,7 @@ fn isa_coverage() { let Ok(mut code) = encode(inst.name, enc, fields, lit) else { continue }; code.extend(encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap()); let ok = std::panic::catch_unwind(|| { - try_mode(&code, Mode::Kernel) || try_mode(&code, Mode::Pixel(super::PsInterface { inputs: vec![], outputs: vec![], input_ena: 0x7F, attrs: None })) + try_mode(&code, Mode::Kernel) || try_mode(&code, Mode::Pixel(super::PsInterface { inputs: vec![], outputs: vec![], input_ena: 0x7F, attrs: None, early_depth: false })) }) .unwrap_or(false); let e = per_enc.entry(enc.trim_start_matches("ENC_")).or_default(); @@ -7951,6 +7956,7 @@ fn ps_interp_p0_preserves_bits_and_p2_converts_to_float() { outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)], input_ena: 0x302, attrs: None, + early_depth: false, }; let pt = translate(&ps_code, &Options { mode: Mode::Pixel(psi), wave64: true, ..Default::default() }).unwrap(); assert!(pt.complete(), "{:?}\n{}", pt.unsupported_why, pt.hlsl);