From 4574f2e04c8f480436e4183121e8f251b264c992 Mon Sep 17 00:00:00 2001 From: Ahmet Dedeler <105894172+Ahmet-Dedeler@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:34:14 +0300 Subject: [PATCH] D3D12.X: stand-in pixel shaders write both dual-source colours A stand-in pipeline keeps the title's blend state, and a title's blend state can use SRC1 factors on render target 0 (dual-source blending). The stand-in pixel shaders wrote SV_Target0 only. Apple's D3DMetal builds the pixel stage for the blend state and refuses such a pair: "Failed to compile fragment function", logged 2,815 times in one session of a retail game, and the pipeline is marked a no-op. Both stand-ins (the DXBC one of `dummy`, the DXIL one of `dummy_mesh`) now write SV_Target0 and SV_Target1 from one source string. Nothing is drawn with them, and without dual-source blending a second output is ignored. Test: standin_pixel_shaders_write_both_dual_source_colours reads the output signature of both blobs. Part of #258. Co-Authored-By: Claude Mythos 5.2 --- crates/xcl/src/hle/d3d12x/real.rs | 33 ++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 26019155e..b27b06ec4 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3079,6 +3079,15 @@ struct Dummy { cs: Vec, } +/// The stand-in pixel shader. Nothing is drawn with it (its vertex / mesh shader makes no +/// pixels), but its pipeline keeps the title's blend state, and that can be dual-source +/// blending (SRC1 factors on target 0: a retail game's forward transparency passes). A host that +/// builds the pixel stage for the blend state needs both colours then: Apple's Metal shader +/// converter (D3DMetal) refuses a pixel shader that writes SV_Target0 alone ("Failed to compile +/// fragment function", the pipeline a no-op). Hence SV_Target1 too; with the usual blending a +/// second output changes nothing. +const STANDIN_PS: &str = "struct O { float4 c0 : SV_Target0; float4 c1 : SV_Target1; };\nO main() { O o; o.c0 = float4(1, 0, 1, 1); o.c1 = float4(0, 0, 0, 0); return o; }\n"; + /// Stand-in mesh shader and pixel shader (DXIL, as mesh pipelines need): one degenerate /// triangle (DXC wants every output written), nothing drawn. fn dummy_mesh() -> Option<&'static (Vec, Vec)> { @@ -3086,8 +3095,7 @@ fn dummy_mesh() -> Option<&'static (Vec, Vec)> { D.get_or_init(|| { use crate::gpu::recomp::dxc::compile; let ms = "struct V { float4 p : SV_Position; };\n[outputtopology(\"triangle\")]\n[numthreads(1, 1, 1)]\nvoid main(out vertices V v[1], out indices uint3 t[1]) { SetMeshOutputCounts(1, 1); v[0].p = float4(0, 0, 0, 1); t[0] = uint3(0, 0, 0); }\n"; - let ps = "float4 main() : SV_Target { return float4(1, 0, 1, 1); }\n"; - match (compile(ms, "main", "ms_6_5", &[]), compile(ps, "main", "ps_6_0", &[])) { + match (compile(ms, "main", "ms_6_5", &[]), compile(STANDIN_PS, "main", "ps_6_0", &[])) { (Ok(m), Ok(p)) => Some((m, p)), (m, p) => { crate::xlog_warn!("D3D12.X: stand-in mesh shaders: {:?} {:?}", m.err(), p.err()); @@ -3127,7 +3135,7 @@ fn dummy() -> &'static Dummy { static D: OnceLock = OnceLock::new(); D.get_or_init(|| Dummy { vs: compile("float4 main(uint id : SV_VertexID) : SV_Position { return float4(0, 0, 0, 1); }", "vs_5_0"), - ps: compile("float4 main() : SV_Target { return float4(1, 0, 1, 1); }", "ps_5_0"), + ps: compile(STANDIN_PS, "ps_5_0"), cs: compile("[numthreads(1, 1, 1)] void main() {}", "cs_5_0"), }) } @@ -5022,6 +5030,25 @@ mod this_first_tests { } } +#[cfg(test)] +mod standin_tests { + use super::*; + + /// The stand-in pixel shaders write SV_Target0 and SV_Target1: the pair a pipeline with a + /// title's dual-source blend state needs (Metal's shader converter refuses SV_Target0 alone). + #[test] + fn standin_pixel_shaders_write_both_dual_source_colours() { + let targets = |b: &[u8]| -> Vec { + crate::gpu::dxbc::signatures(b).1.iter().filter(|e| e.name.eq_ignore_ascii_case("SV_Target")).map(|e| e.index).collect() + }; + assert_eq!(targets(&dummy().ps), [0, 1], "the DXBC stand-in"); + match dummy_mesh() { + Some((_, ps)) => assert_eq!(targets(ps), [0, 1], "the DXIL stand-in of mesh pipelines"), + None => crate::gpu::recomp::runner::skip_test("no DXC"), + } + } +} + #[cfg(test)] mod cache_report_tests { use super::*;