From f6c6d6bf5ed97685b139150bb88b4f9429b16719 Mon Sep 17 00:00:00 2001 From: veyn-sudo <360844188+veyn-sudo@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:16:07 +0100 Subject: [PATCH 1/2] D3D12.X: a console sampler keeps its filter CreateSamplerX made every sampler anisotropic, so point samplers filtered linearly. The desc has the filters at +4 / +8 (magnification, minification), +0xC (z) and +0x10 (mip), numbered as in an S#. Of the 30 different descs a retail title makes up to its first screen, 5 are point (2 2 1 1), 17 bilinear (1 1 1 2, 1 1 2 2) and 8 anisotropic (3 3 2 2; one of them with a maximum of 1). None has a comparison function. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/device_x.rs | 72 ++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 8dec7b1c2..9612700b7 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -621,17 +621,23 @@ pub(super) unsafe extern "system" fn unregister_page_pool_x(_this: *mut c_void, } } -/// device #61 CreateSamplerX(desc, destination): the title uses it for anisotropic samplers; -/// the console desc (0x6C bytes) carries the PC fields at other offsets (+0x18 address U / V / -/// W, +0x24 / +0x28 min / max LOD, +0x2C bias, +0x34 max anisotropy, +0x40 comparison); its -/// further anisotropy fields are not modelled -pub(super) unsafe extern "system" fn create_sampler_x(this: *mut c_void, x: *const u8, dest: u64) { - if x.is_null() || !crate::gpu::shader::readable(x as usize, 0x6C) { - return; - } - let u = |o: usize| (x.add(o) as *const u32).read_unaligned(); - let d = D3D12_SAMPLER_DESC { - Filter: D3D12_FILTER_ANISOTROPIC, +/// PC filter of a console sampler desc. The fields are numbered as in an S#: magnification and +/// minification 1 bilinear, 2 anisotropic point, 3 anisotropic bilinear, mip 2 linear. +fn sampler_x_filter(mag: u32, min: u32, mip: u32, max_anisotropy: u32) -> D3D12_FILTER { + if max_anisotropy > 1 && (mag == 3 || min == 3) { + return D3D12_FILTER_ANISOTROPIC; + } + // D3D12_ENCODE_BASIC_FILTER: linear = 1 + D3D12_FILTER(((min & 1) << 4 | (mag & 1) << 2 | (mip == 2) as u32) as i32) +} + +/// PC sampler desc of a console one (0x6C bytes; `u`: the dword at an offset). It carries the PC +/// fields at other offsets (+4 / +8 / +0x10 magnification / minification / mip filter, +0x18 +/// address U / V / W, +0x24 / +0x28 min / max LOD, +0x2C bias, +0x34 max anisotropy, +0x40 +/// comparison); its further anisotropy fields are not modelled. +fn pc_sampler_desc(u: impl Fn(usize) -> u32) -> D3D12_SAMPLER_DESC { + D3D12_SAMPLER_DESC { + Filter: sampler_x_filter(u(4), u(8), u(0x10), u(0x34)), AddressU: D3D12_TEXTURE_ADDRESS_MODE(u(0x18) as i32), AddressV: D3D12_TEXTURE_ADDRESS_MODE(u(0x1C) as i32), AddressW: D3D12_TEXTURE_ADDRESS_MODE(u(0x20) as i32), @@ -641,7 +647,15 @@ pub(super) unsafe extern "system" fn create_sampler_x(this: *mut c_void, x: *con BorderColor: [0.0; 4], MinLOD: f32::from_bits(u(0x24)), MaxLOD: f32::from_bits(u(0x28)), - }; + } +} + +/// device #61 CreateSamplerX(desc, destination) +pub(super) unsafe extern "system" fn create_sampler_x(this: *mut c_void, x: *const u8, dest: u64) { + if x.is_null() || !crate::gpu::shader::readable(x as usize, 0x6C) { + return; + } + let d = pc_sampler_desc(|o| (x.add(o) as *const u32).read_unaligned()); real::dev_create_sampler_x(this, &d, dest); } @@ -1059,6 +1073,40 @@ pub(super) unsafe extern "system" fn create_command_signature_x(this: *mut c_voi mod tests { use super::*; + /// A console sampler desc: [mag, min, z, mip] filters, address U / V / W, maximum anisotropy. + fn console_sampler(filters: [u32; 4], address: [u32; 3], anisotropy: u32) -> [u32; 0x6C / 4] { + let mut d = [0u32; 0x6C / 4]; + d[1..5].copy_from_slice(&filters); + d[6..9].copy_from_slice(&address); + d[0x28 / 4] = 12f32.to_bits(); + d[0x34 / 4] = anisotropy; + d[0x40 / 4] = 1; + d + } + + /// Only an anisotropic console sampler is an anisotropic PC one (all of them were). + #[test] + fn console_samplers_keep_their_filter() { + let pc = |filters, anisotropy| { + let d = console_sampler(filters, [1, 2, 3], anisotropy); + pc_sampler_desc(|o| d[o / 4]) + }; + assert_eq!(pc([2, 2, 1, 1], 1).Filter, D3D12_FILTER_MIN_MAG_MIP_POINT); + assert_eq!(pc([1, 1, 2, 2], 1).Filter, D3D12_FILTER_MIN_MAG_MIP_LINEAR); + assert_eq!(pc([1, 1, 1, 1], 1).Filter, D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT); + // the z filter is not the mip filter, and mag and min are not swapped + assert_eq!(pc([1, 1, 1, 2], 1).Filter, D3D12_FILTER_MIN_MAG_MIP_LINEAR); + assert_eq!(pc([1, 2, 1, 1], 1).Filter, D3D12_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT); + let d = pc([3, 3, 2, 2], 8); + assert_eq!((d.Filter, d.MaxAnisotropy), (D3D12_FILTER_ANISOTROPIC, 8)); + // a maximum of 1 is trilinear, and anisotropic point is point + assert_eq!(pc([3, 3, 2, 2], 1).Filter, D3D12_FILTER_MIN_MAG_MIP_LINEAR); + assert_eq!(pc([2, 2, 1, 1], 8).Filter, D3D12_FILTER_MIN_MAG_MIP_POINT); + // the other fields as before + let d = pc([1, 1, 2, 2], 1); + assert_eq!((d.AddressU.0, d.AddressV.0, d.AddressW.0, d.MinLOD, d.MaxLOD, d.ComparisonFunc.0), (1, 2, 3, 0.0, 12.0, 1)); + } + #[test] fn console_types_and_descs_become_pc_ones() { assert_eq!(pc_list_type(XBOX_TYPE_DMA), D3D12_COMMAND_LIST_TYPE_COPY.0 as u32); From b76c075450a3bee4d4d27ad6e9de06d0e308f933 Mon Sep 17 00:00:00 2001 From: veyn-sudo <360844188+veyn-sudo@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:16:41 +0100 Subject: [PATCH 2/2] D3D12.X: a console sampler keeps its border colour CreateSamplerX gave every sampler a transparent black border. The desc has the colour at +0x44: of a retail title's 9 descs with border addressing, one asks for opaque black and one for white. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/device_x.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 9612700b7..96b135432 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -634,7 +634,7 @@ fn sampler_x_filter(mag: u32, min: u32, mip: u32, max_anisotropy: u32) -> D3D12_ /// PC sampler desc of a console one (0x6C bytes; `u`: the dword at an offset). It carries the PC /// fields at other offsets (+4 / +8 / +0x10 magnification / minification / mip filter, +0x18 /// address U / V / W, +0x24 / +0x28 min / max LOD, +0x2C bias, +0x34 max anisotropy, +0x40 -/// comparison); its further anisotropy fields are not modelled. +/// comparison, +0x44 border colour); its further anisotropy fields are not modelled. fn pc_sampler_desc(u: impl Fn(usize) -> u32) -> D3D12_SAMPLER_DESC { D3D12_SAMPLER_DESC { Filter: sampler_x_filter(u(4), u(8), u(0x10), u(0x34)), @@ -644,7 +644,7 @@ fn pc_sampler_desc(u: impl Fn(usize) -> u32) -> D3D12_SAMPLER_DESC { MipLODBias: f32::from_bits(u(0x2C)), MaxAnisotropy: u(0x34).clamp(1, 16), ComparisonFunc: D3D12_COMPARISON_FUNC(u(0x40) as i32), - BorderColor: [0.0; 4], + BorderColor: [0x44, 0x48, 0x4C, 0x50].map(|o| f32::from_bits(u(o))), MinLOD: f32::from_bits(u(0x24)), MaxLOD: f32::from_bits(u(0x28)), } @@ -1107,6 +1107,18 @@ mod tests { assert_eq!((d.AddressU.0, d.AddressV.0, d.AddressW.0, d.MinLOD, d.MaxLOD, d.ComparisonFunc.0), (1, 2, 3, 0.0, 12.0, 1)); } + /// The border colour is the desc's (it was transparent black whatever the title asked for). + #[test] + fn console_samplers_keep_their_border_colour() { + let mut d = console_sampler([1, 1, 2, 2], [4; 3], 1); + let border = |d: &[u32; 0x6C / 4]| pc_sampler_desc(|o| d[o / 4]).BorderColor; + assert_eq!(border(&d), [0.0; 4]); + for (k, v) in [0.25f32, 0.5, 0.75, 1.0].into_iter().enumerate() { + d[0x44 / 4 + k] = v.to_bits(); + } + assert_eq!(border(&d), [0.25, 0.5, 0.75, 1.0]); + } + #[test] fn console_types_and_descs_become_pc_ones() { assert_eq!(pc_list_type(XBOX_TYPE_DMA), D3D12_COMMAND_LIST_TYPE_COPY.0 as u32);