diff --git a/TODO.md b/TODO.md index 952d23588..0a40a61d0 100644 --- a/TODO.md +++ b/TODO.md @@ -25,7 +25,7 @@ The goal: a console-like experience on PC. The game starts straight into play, h | R-4 | An indirect compute pass asks for about 77,500 thread groups in x, where one dispatch runs 65,535. The rest is cut off; split the dispatch. | `leonida-gpu` | game, GPU | | R-5 | Real queries, depth bounds, HDR output and scaling are missing. | `leonida-gpu` | game, GPU | | R-6 | Render-target metadata planes are not handled. | `leonida-formats`, `leonida-gpu` | game, GPU | -| R-7 | Shaders that don't translate drop their draws. Count them again on this tree with `leonida translate --list` (the upstream counts in the fork notes are from 2026-11-23) and work down the list. | `leonida-shader` | game | +| R-7 | Shaders that don't translate drop their draws. Count them again on this tree with `leonida translate --list` (the upstream counts in the fork notes are from 2026-11-23) and work down the list. Counted 2026-11-26 in the fork notes: the 195 `GS_VoxelizeGeneric` failures drop no draws (geometry programs are never translated); the 5 `CS_TraceShadowRays_*` need view addresses by heap slot from `leonida-gpu`. | `leonida-shader` | game | | R-8 | Mapping pool pages in a row as one view made remaps cheaper but broke texture streaming (black or red faces, garbage in 3 of 3 runs). Find out why before trying it again; see P-4. | `leonida-hle` (`xmem`) | game, GPU | ## Performance: a steady 30 fps diff --git a/crates/leonida-shader/src/emit/lower.rs b/crates/leonida-shader/src/emit/lower.rs index d21358893..1ca6d92c9 100644 --- a/crates/leonida-shader/src/emit/lower.rs +++ b/crates/leonida-shader/src/emit/lower.rs @@ -2363,6 +2363,9 @@ impl<'a> Lower<'a> { Input::ThreadIdInGroup(c) => return self.thread_id(c), Input::GroupId(c) => return self.compute_id("groupId.i32", 94, c), Input::GroupInfo => return Val::Missing("the thread-group info SGPR"), + // shortcut: the host orders nothing; bit 31 clear says the wave + // overlaps no earlier one, so the program skips its wait. + Input::PopsCollision => return Val::I32(self.s.m.i32(0)), Input::MeshGroup => { let Val::I32(g) = self.compute_id("groupId.i32", 94, 0) else { unreachable!() @@ -3181,6 +3184,9 @@ impl<'a> Lower<'a> { let v = inst.read(Reg::V(reg)).ok_or("an unread operand")?; self.i32_of(v) } + // shortcut: no wave ever leaves an ordered section on the host + // ([`Input::PopsCollision`] says none overlaps). + Operand::Const(crate::io::POPS_EXITING_WAVE_ID) => Ok(self.s.m.i32(0)), Operand::Const(c) => crate::resource::inline(c) .map(|x| self.s.m.i32(x)) .ok_or("a special operand (vccz, execz, scc, lds_direct)"), diff --git a/crates/leonida-shader/src/io.rs b/crates/leonida-shader/src/io.rs index 09b1f8e69..f9698ce41 100644 --- a/crates/leonida-shader/src/io.rs +++ b/crates/leonida-shader/src/io.rs @@ -70,6 +70,10 @@ pub enum Input { MeshGroup, /// Mesh: s3, the wave's index in the group in bits 24-27. MergedWaveInfo, + /// Pixel: the SGPR after the primitive mask in a program that orders + /// overlapping pixels (POPS): the packer, the wave ids to wait for, and + /// in bit 31 whether the wave overlaps an earlier one. + PopsCollision, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -204,10 +208,23 @@ pub fn raise(p: &Program, native: &NativeProgram) -> Result { HwStage::Ps, ) => { // A pixel shader exports no parameters; `[]` never reaches `Unread`. - let loaded = ps_vgprs(*input_addr) + let mut loaded: Vec<(Reg, Input)> = ps_vgprs(*input_addr) .into_iter() .map(|(r, i)| (Reg::V(r), i)) .collect(); + // The user SGPRs, then the primitive mask, then (ordered pixels + // only) the collision SGPR. + if orders_pixels(p) { + let user = native + .layout + .iter() + .map(|r| u32::from(r.user_data) + r.sgprs) + .max() + .unwrap_or(0); + if let Ok(s) = u8::try_from(user + 1) { + loaded.push((Reg::S(s), Input::PopsCollision)); + } + } (loaded, &inputs[..], &[][..]) } (StageIo::Vs { inputs, outputs }, HwStage::Vs) => { @@ -463,6 +480,23 @@ pub fn raise(p: &Program, native: &NativeProgram) -> Result { Ok(io) } +/// `src_pops_exiting_wave_id`: the id of the last wave that left the +/// ordered section. +pub(crate) const POPS_EXITING_WAVE_ID: u8 = 239; + +/// Whether `p` waits for the earlier waves of overlapping pixels (POPS): +/// it reads [`POPS_EXITING_WAVE_ID`]. +fn orders_pixels(p: &Program) -> bool { + p.blocks.iter().flat_map(|b| &b.insts).any(|i| { + let src: &[Operand] = match &i.fields { + Fields::Sop { src, .. } => src, + Fields::Valu(v) => &v.src, + _ => &[], + }; + src.contains(&Operand::Const(POPS_EXITING_WAVE_ID)) + }) +} + /// The pixel input VGPRs that `addr` enables, with their meaning. fn ps_vgprs(addr: u32) -> Vec<(u8, Input)> { let mut out = Vec::new(); @@ -725,6 +759,32 @@ mod tests { ); } + #[test] + fn ordered_pixels_read_the_collision_sgpr() { + // One user SGPR at s2: the primitive mask in s3, the collision in s4. + let table = crate::native::RootParam { + root: crate::native::RootType::Table, + table_offset_dwords: 0, + sgprs: 1, + base_register: 0, + user_data: 2, + slot: 0, + }; + let mut native = pixel(vec![], 0); + native.layout = vec![table]; + let ordered = program(&[ + 0x9000_9F04, // s_lshr_b32 s0, s4, 31 + 0x8102_01EF, // s_add_i32 s2, src_pops_exiting_wave_id, s1 + S_ENDPGM, + ]); + let io = raise(&ordered, &native).unwrap(); + let s4 = ordered.inputs.iter().find(|i| i.0 == Reg::S(4)).unwrap().1; + assert_eq!(io.inputs, [(s4, Input::PopsCollision)]); + // Without the wait s4 is no input the hardware loads. + let plain = program(&[0x9000_9F04, S_ENDPGM]); + assert!(raise(&plain, &native).unwrap().inputs.is_empty()); + } + #[test] fn mesh_inputs_and_primitive_export() { let mesh = |words: &[u32]| { diff --git a/docs/fork-notes.md b/docs/fork-notes.md index ac3a6ca83..ac3b6b9a0 100644 --- a/docs/fork-notes.md +++ b/docs/fork-notes.md @@ -127,6 +127,21 @@ eight base settings. DXVK reports an AMD vendor ID for this NVIDIA card; profiles intentionally use the adapter identity exposed to Direct3D. No game files were used in these checks. +**R-7: untranslated shaders counted again; ordered-pixel shaders translate.** +`leonida translate --list` on this tree: 26,148 programs, 282 failures +(614 duplicates skipped). 195 are `GS_VoxelizeGeneric` geometry programs +("an address in an SGPR that is no root parameter"); they drop no draws, +since only vertex, pixel, compute and mesh programs are translated. 40 are +"a descriptor chosen by control flow", 29 containers have no DXIL part. +The 10 "a hardware SGPR input" were pixel shaders that wait for earlier +waves of overlapping pixels (POPS): they read the SGPR after the primitive +mask and `src_pops_exiting_wave_id`. Both now read as "no overlap" +(`shortcut:`: the host orders nothing), and all 10 translate and validate. +The 5 `CS_TraceShadowRays_*` ("a descriptor read only as data") load a +buffer view from the resource heap at a computed index and walk the memory +behind its address; that needs view addresses by heap slot from the host, +as `SLOT_STRIDES` gives strides (not done). + **P-5: less clock work in emulated wait hints.** Each `monitorx` / `mwaitx` pair previously read and converted the performance counter three times: once for each rate-line check and once for wait