From 3dfa8503d3cdcf70baa1504986198386111c6b0f Mon Sep 17 00:00:00 2001 From: Saurabh Suresh Date: Fri, 27 Nov 2026 01:54:08 +0400 Subject: [PATCH] fix(shader): test depth first where the console's pixel shader does A console pixel shader built with [earlydepthstencil] sets DB_SHADER_CONTROL.DEPTH_BEFORE_SHADER (bit 12): pixels behind closer geometry never run it. The host tests depth after any shader that writes UAVs, so those pixels' writes landed. GTA VI's alpha prepass writes velocity through a UAV; the window behind the player overwrote her motion vectors inside the window's rectangle. Such shaders now get ForceEarlyDepthStencil, unless they export depth or stencil. Co-Authored-By: Claude Opus 5.5 --- crates/victory-shader/src/emit/lower.rs | 25 +++++- crates/victory-shader/src/emit/lower/tests.rs | 77 +++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index c8fe9dd96..a3280d740 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -428,6 +428,9 @@ struct Lower<'a> { /// Pixel: an ordered section (POPS), whose UAVs the host makes /// rasterizer-ordered views. pops: bool, + /// Pixel: the console tests depth and stencil before the shader runs + /// (`DB_SHADER_CONTROL.DEPTH_BEFORE_SHADER`, HLSL `[earlydepthstencil]`). + depth_before_shader: bool, /// The program uses title memory (addresses it computes, the BVH /// instruction, root parameters through the user-data pointer): its root /// SRVs and UAVs are read there too ([`Lower::raw_view`]). @@ -936,6 +939,8 @@ impl<'a> Lower<'a> { }, pops: matches!(x.native.io, StageIo::Ps { db_shader_control, .. } if db_shader_control & 1 << 16 != 0), + depth_before_shader: matches!(x.native.io, StageIo::Ps { db_shader_control, .. } + if db_shader_control & 1 << 12 != 0), root_memory, flat: None, no_barrier, @@ -1680,18 +1685,34 @@ impl<'a> Lower<'a> { // only UAV is VIctory's own read-mark bitmap (`mark_view_read`): // without ForceEarlyDepthStencil, the injected UAV forces late-Z and // shades occluded pixels behind closer geometry. + let exports_depth = (self.s.flags & flags::STENCIL_REF) != 0 + || self + .s + .outputs + .iter() + .any(|o| matches!(o.kind, 14 | 17..=20)); if self.s.pixel && self.s.view_writes.is_some() && self.s.root_uavs.is_empty() && self.s.memory_dirty.is_none() && !self.uav_handle && !self.pops - && (self.s.flags & (flags::ROVS | flags::STENCIL_REF)) == 0 + && (self.s.flags & flags::ROVS) == 0 && !self.has_discard - && !self.s.outputs.iter().any(|o| matches!(o.kind, 14 | 17..=20)) + && !exports_depth { self.s.flags |= flags::EARLY_DEPTH_STENCIL; } + // The console tests depth before such a shader runs: pixels behind + // closer geometry never run it, so what it writes through views or + // title memory stays as the console leaves it. The host tests depth + // after any shader that writes, and there those pixels' writes land + // (GTA VI's alpha prepass wrote the window's velocity over the + // player in front of it). A shader exporting depth cannot test it + // first; the console does not combine the two either. + if self.s.pixel && self.depth_before_shader && !exports_depth { + self.s.flags |= flags::EARLY_DEPTH_STENCIL; + } #[cfg(test)] let raw_loads = { let (m, name) = (&self.s.m, "dx.op.rawBufferLoad.f32"); diff --git a/crates/victory-shader/src/emit/lower/tests.rs b/crates/victory-shader/src/emit/lower/tests.rs index 2a0a8322b..5a98c095e 100644 --- a/crates/victory-shader/src/emit/lower/tests.rs +++ b/crates/victory-shader/src/emit/lower/tests.rs @@ -2525,6 +2525,83 @@ fn pixel_shader_skips_early_depth_stencil_with_discard_depth_or_uav() { assert!(!uav.early_depth_stencil); } +/// `DB_SHADER_CONTROL.DEPTH_BEFORE_SHADER`: the console tests depth before +/// the pixel shader, so a pixel behind closer geometry never makes the +/// shader's writes. The host shader then tests depth first too, title UAV +/// writes and discards notwithstanding; a depth export still keeps it late. +#[test] +fn console_depth_before_shader_keeps_early_depth_stencil_with_uav_writes() { + const DEPTH_BEFORE_SHADER: u32 = 1 << 12; + let uav_store = [ + 0xF408_0202, // s_load_dwordx4 s[8:11], s[4:5], 64 + 0xFA00_0040, + 0xBF8C_0000, // s_waitcnt lgkmcnt(0) + 0xE070_0000, // buffer_store_dword v2, off, s[8:11], 0 + 0x8002_0000, + ]; + let color = [0xF800_180F, 0x0504_0302]; // exp mrt0 v2, v3, v4, v5 done vm + for view_marks in [false, true] { + let uav = lower_ps_for_early_z( + &uav_store, + color, + Vec::new(), + DEPTH_BEFORE_SHADER, + view_marks, + ); + assert!( + uav.early_depth_stencil, + "title UAV store, view marks {view_marks}" + ); + #[cfg(windows)] + if let Ok(mut v) = crate::emit::validator::Validator::load( + crate::emit::validator::Validator::default_path().as_deref(), + ) { + v.validate(&mut uav.dxil.clone()).unwrap(); + } + } + + // Conditional discard (as in `pixel_shader_skips_..._or_uav`). + let discard = lower_ps_for_early_z( + &[ + 0xBE88_037E, // s_mov_b32 s8, exec_lo + 0x7C22_0480, // v_cmpx_lt_f32 exec, 0, v2 + 0x8A08_7E08, // s_andn2_b32 s8, s8, exec_lo + 0xBF84_0004, // s_cbranch_scc0 -> kill + 0xBEFE_0308, // s_mov_b32 exec_lo, s8 + 0xF800_180F, // exp mrt0 v2, v3, v4, v5 done vm + 0x0504_0302, + S_ENDPGM, + ], + [0xF800_1890, 0x0000_0000], // kill: exp null done vm + Vec::new(), + DEPTH_BEFORE_SHADER, + true, + ); + assert!(discard.early_depth_stencil, "discard"); + + // Depth export (exp mrtz v2, off, off, off done vm). + let depth_out = vec![crate::emit::container::SigElement { + name: "SV_Depth".into(), + index: 0, + kind: 17, + comp: 9, + interp: 0, + row: None, + col: 0, + cols: 1, + usage: 1, + half: false, + }]; + let depth = lower_ps_for_early_z( + &[], + [0xF800_1881, 0x0000_0002], + depth_out, + DEPTH_BEFORE_SHADER, + true, + ); + assert!(!depth.early_depth_stencil, "depth export"); +} + /// GLOBAL `op` with address `s[saddr:saddr+1] + vaddr + offset`. const fn global(op: u32, vdst: u8, vaddr: u8, saddr: u8, offset: u32) -> [u32; 2] { [