From c8d4a2fd26a9e0c7eb366fba676cd1a3719b4cb9 Mon Sep 17 00:00:00 2001 From: Foniass <107039406+Foniass@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:12:04 +0100 Subject: [PATCH 1/2] =?UTF-8?q?v=5Ffract=5Ff32:=20=C2=B1inf=20gives=20NaN?= =?UTF-8?q?=20and=20a=20NaN=20stays=20NaN,=20as=20on=20RDNA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fract_f clamped x - floor(x) with min(), and HLSL min returns its non-NaN operand: fract(±inf) and fract(NaN) came out 0.99999994 (measured on an RTX 5070 Ti). RDNA (GFX8 and later) computes x - floor(x) and keeps it below 1.0, so ±inf gives NaN and NaN passes through; a compare-and-select does the same. Test: gpu_fract_f32_edges (fails with the old helper). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/emit.rs | 4 +++- crates/xcl/src/gpu/recomp/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 3ebd7bc..8cbbf6d 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4535,7 +4535,9 @@ float mad_nf(float a, float b, float c) { precise float p = a * b; precise float float med3f(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); } uint med3u(uint a, uint b, uint c) { return max(min(a, b), min(max(a, b), c)); } int med3i(int a, int b, int c) { return max(min(a, b), min(max(a, b), c)); } -float fract_f(float x) { return min(x - floor(x), asfloat(0x3F7FFFFFu)); } +// x - floor(x) below 1.0 (RDNA: a NaN stays NaN and ±inf gives NaN – min() would return the +// clamp for both: HLSL min returns its non-NaN operand) +float fract_f(float x) { float r = x - floor(x); return select(r >= 1.0f, asfloat(0x3F7FFFFFu), r); } uint cvt_u32(float f) { return select(or(isnan(f), f <= 0.0f), 0u, select(f >= 4294967296.0f, 0xFFFFFFFFu, (uint)f)); } int cvt_i32(float f) { return select(isnan(f), 0, select(f <= -2147483648.0f, (int)0x80000000u, select(f >= 2147483648.0f, 0x7FFFFFFF, (int)f))); } int i24(uint v) { return ((int)(v << 8)) >> 8; } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 5a927c5..17a5f67 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -4098,3 +4098,30 @@ fn mesh_wave32_rejects_cross_half_operations() { assert!(split.hlsl.contains("xcl_logical_wave64 = true;")); assert!(split.complete(), "{:?}", split.unsupported_why); } + +/// v_fract_f32 at the edges, as RDNA computes it (x - floor(x) kept below 1.0): ±inf gives +/// NaN and a NaN stays NaN – a `min()` clamp returned 0.99999994 there on NVIDIA (HLSL min +/// returns its non-NaN operand); a tiny negative value rounds to the largest float below 1. +#[test] +fn gpu_fract_f32_edges() { + if !gpu_ready() { + return; + } + let code = prog(&[("V_FRACT_F32", "ENC_VOP1", &[("VDST", 2), ("SRC0", V + 1)], None)]); + let inputs = [f32::INFINITY, f32::NEG_INFINITY, f32::NAN, -0.0, 1.5, -0.25, -(2f32.powi(-25)), 7.0]; + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[1] = inputs[l % inputs.len()].to_bits(); + } + let r = runner::run_code(&code, &st, &[]).unwrap(); + for l in 0..64 { + let (x, got) = (inputs[l % inputs.len()], f32::from_bits(r.vgpr[l][2])); + if x.is_infinite() || x.is_nan() { + assert!(got.is_nan(), "lane {l}: fract({x}) = {got}, want NaN"); + } else if x == -(2f32.powi(-25)) { + assert_eq!(r.vgpr[l][2], 0x3F7F_FFFF, "lane {l}: fract({x}) below 1.0"); + } else { + assert_eq!(got.to_bits(), (x - x.floor()).to_bits(), "lane {l}: fract({x})"); + } + } +} From 319bcd612718593678892275f119f83697b22713 Mon Sep 17 00:00:00 2001 From: Foniass <107039406+Foniass@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:16:18 +0100 Subject: [PATCH 2/2] Console ResourceBarrier: UAV barriers for all resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Translated shaders reach title memory through XCL's memory window buffers, other D3D12 resources over the same memory as the title's buffers and textures. A UAV barrier the title gives for its own resource, or a transition out of / into UNORDERED_ACCESS, therefore did not order the shaders' window accesses on the PC: AMD drivers wait and flush for any UAV barrier, NVIDIA's need not – a possible cause of NVIDIA-only flicker (a pass reading what the previous one is still writing). Such a batch now carries one UAV barrier with no resource (all UAV accesses), which costs about the same as one per resource. Not reproduced without a game; the test title's lists go through this path and pass. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/list_x.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 4d8feee..791b164 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1649,7 +1649,9 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 if barriers.is_null() || n == 0 { return; } - let mut out: Vec<[u32; 8]> = Vec::with_capacity(n as usize); + let mut out: Vec<[u32; 8]> = Vec::with_capacity(n as usize + 1); + // a UAV barrier for all resources is in `out` / a transition from or to UNORDERED_ACCESS + let (mut any_uav, mut uav_transition) = (false, false); for k in 0..n as usize { let mut b = barriers.add(k).read_unaligned(); let (ty, flags) = (b[0], b[1]); @@ -1674,6 +1676,17 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 // (the list would fail to Close) – an aliasing barrier for any resources instead b[2..6].fill(0); } + if ty == 2 { + // UAV (resource +8): translated shaders reach title memory through XCL's memory + // window buffers – other resources over the same memory – which a UAV barrier for + // the title's resource alone does not order (AMD drivers wait and flush for any UAV + // barrier, NVIDIA's need not): a UAV barrier for all resources, once per batch + b[2..4].fill(0); + if any_uav { + continue; + } + any_uav = true; + } if ty == 0 { // transition: resource (+8), subresource (+16), before (+20), after (+24) let (before, after) = (b[5], b[6]); @@ -1692,9 +1705,14 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 if b[5] == b[6] { continue; } + // UAV writes before (or after) the transition: the same holds for the window buffers + uav_transition |= (b[5] | b[6]) & UNORDERED_ACCESS != 0; } out.push(b); } + if uav_transition && !any_uav { + out.push([2, 0, 0, 0, 0, 0, 0, 0]); + } if !out.is_empty() { pc::(this, PC_RESOURCE_BARRIER)(this, out.len() as u32, out.as_ptr()); } @@ -1703,6 +1721,9 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 /// D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER | INDEX_BUFFER: valid for buffers only const BUFFER_ONLY_STATES: u32 = 0x1 | 0x2; +/// D3D12_RESOURCE_STATE_UNORDERED_ACCESS +const UNORDERED_ACCESS: u32 = 0x8; + /// Whether `res` is a buffer (null / unknown: taken as one – its states are left alone). unsafe fn is_buffer(res: usize) -> bool { let r = res as *mut c_void;