From 724cd950a8f4499f98661491a0ca62c014177e53 Mon Sep 17 00:00:00 2001 From: Nick Stallman Date: Thu, 26 Nov 2026 17:32:38 +1100 Subject: [PATCH] dxbc, xshader: read four-word typed UAVs in a mesh pipeline's pixel layout ProjectScarlett's most common translator refusal after loading into the world is `unresolved image [image_sample_lz]` (241 recorded requests, about 170 seen in one run). Every one is a mesh pipeline's pixel program translated against the title's root. That root does not describe the program's table: the program samples t91 at s8 + 0x1740 (slot 186, per its FSSM record and PSV), where the root declares u7. The emitter resolves the slot to a UAV and cannot sample it. #177 lets the runtime lay out such a table as the pixel program reads it (`MeshLayout::from_pixel_shader`, `ComputeTable::pixel_root`). These programs also record typed UAVs with four words and shape 4 (class 0x303), which `read_stage` did not accept, so the pixel layout was dropped and the guest root kept. Accept that record in pixel programs when PSV names the register as a typed UAV and not a structured one. The emitter now names the mismatch instead of "unresolved image" when a sample or gather resolves to a table UAV. Against the recorded requests, with the pixel root the runtime would now build (simulated from the root-less program and the request's root): 240 of the 241 translate; one has another root layout. The recorded rooted requests are unchanged apart from the refusal text (19,948 of 21,867 translate before and after). Co-Authored-By: Claude Opus 5.5 --- runtime/dxbc/src/mesh_table.rs | 3 ++ runtime/dxbc/src/tests/mesh_table.rs | 46 +++++++++++++++++++ .../shader-compiler/src/hlsl/emitter/image.rs | 5 ++ .../src/tests/hlsl/sampler_heap.rs | 38 +++++++++++++++ 4 files changed, 92 insertions(+) diff --git a/runtime/dxbc/src/mesh_table.rs b/runtime/dxbc/src/mesh_table.rs index b607dadc..522b94c7 100644 --- a/runtime/dxbc/src/mesh_table.rs +++ b/runtime/dxbc/src/mesh_table.rs @@ -117,6 +117,9 @@ impl MeshLayout { // An eight-word descriptor is a texture. (0x300, 8, _) => (3, 1), (0x303, 8, _) if pixel => (6, 1), + // Typed unordered-access views can also record four words, textures + // included (ProjectScarlett pixel programs load those as eight). + (0x303, 4, 4) if pixel && named(6) == 1 && named(8) == 0 => (6, 1), (0x300, 4, n) if (4..=2048).contains(&n) && n % 4 == 0 => (5, 1), (0x302, 4, 0) => (2, 1), (0x100, 4, 1) => (4, 2), diff --git a/runtime/dxbc/src/tests/mesh_table.rs b/runtime/dxbc/src/tests/mesh_table.rs index 5d59e34c..0d704af6 100644 --- a/runtime/dxbc/src/tests/mesh_table.rs +++ b/runtime/dxbc/src/tests/mesh_table.rs @@ -187,3 +187,49 @@ fn pixel_layout_replaces_a_table_the_root_declares_one_slot_short() { let slots: Vec<_> = ranges.iter().map(|r| (r.range_type, r.base_register, r.space, r.offset_in_table)).collect(); assert_eq!(slots, [(0, 75, 1, 2), (2, 5, 1, 3)]); } + +#[test] +fn pixel_layout_reads_four_word_typed_unordered_access_views() { + // ProjectScarlett: the program samples t91 at slot 2 and records the typed + // view u13 with four words at slot 3. The root declares u7 at slot 2. + let build = |rows: [[u32; 5]; 2]| { + let mut program = vec![0; 0x100]; + program[..8].copy_from_slice(b"FSSM\x01\x01\0\0"); + for (field, count, rows) in [(0xe8, 0xf8, [[3, 16, 1, 91, 0x008u32], [3, 24, 1, 13, 0x008]]), (0xf0, 0xfc, rows)] { + let offset = (program.len() - 16) as u64; + program[field..field + 8].copy_from_slice(&offset.to_le_bytes()); + program[count..count + 4].copy_from_slice(&2u32.to_le_bytes()); + program.extend(rows.into_iter().flatten().flat_map(u32::to_le_bytes)); + } + let size = (program.len() - 16) as u32; + program[8..12].copy_from_slice(&size.to_le_bytes()); + program + }; + let program = build([[0x300, 16, 91, 4, 0x00808], [0x303, 24, 13, 4, 0x00804]]); + let range = |range_type, base_register| RootRange { range_type, num: 1, base_register, space: 0, offset_in_table: u32::MAX }; + let root = RootSig { + version: 2, + flags: 0xc00, + params: vec![RootParam { + visibility: 0, + kind: RootParamKind::Table(vec![range(0, 301), range(0, 325), range(1, 7)]), + }], + num_static_samplers: 0, + static_samplers: Vec::new(), + }; + let resources = [[3, 0, 91, 91], [6, 0, 13, 13]]; + let layout = MeshLayout::read_stage(&program, &resources, &root, true).unwrap(); + let view = layout.tables[&(16, 96)]; + assert_eq!((view.kind, view.register, view.space), (1, 13, 0)); + let RootParamKind::Table(ranges) = &layout.replacements[&0] else { + panic!("expected a table") + }; + let slots: Vec<_> = ranges.iter().map(|r| (r.range_type, r.base_register, r.offset_in_table)).collect(); + assert_eq!(slots, [(0, 91, 2), (1, 13, 3)]); + // Only a pixel program holds such a view, and only a typed one. + assert!(MeshLayout::read(&program, &resources, &root).is_none()); + assert!(MeshLayout::read_stage(&program, &[[3, 0, 91, 91], [8, 0, 13, 13]], &root, true).is_none()); + // Other four-word unordered-access shapes stay unrecognised. + let structured = build([[0x300, 16, 91, 4, 0x00808], [0x303, 24, 13, 16, 0x00804]]); + assert!(MeshLayout::read_stage(&structured, &resources, &root, true).is_none()); +} diff --git a/tools/shader-compiler/src/hlsl/emitter/image.rs b/tools/shader-compiler/src/hlsl/emitter/image.rs index 69c86267..16df4bc6 100644 --- a/tools/shader-compiler/src/hlsl/emitter/image.rs +++ b/tools/shader-compiler/src/hlsl/emitter/image.rs @@ -318,6 +318,11 @@ impl<'a> Emitter<'a> { }; (self.bind_heap(view, src), dim) } + // The root names a UAV at the table slot the program samples: that root + // does not describe the program's table (see `MeshLayout::from_pixel_shader`). + Some(Res::Uav(slot, _)) if ins.mnemonic.starts_with("image_sample") || ins.mnemonic.starts_with("image_gather") => { + return Err(format!("{} reads table UAV u{slot}: the root does not describe the program's table", ins.mnemonic)); + } _ => return Err("unresolved image".into()), }; if ins.mnemonic == "image_get_resinfo" { diff --git a/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs b/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs index b6c35ab6..f01760b0 100644 --- a/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs +++ b/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs @@ -336,3 +336,41 @@ fn vertex_table_parameter_follows_the_shaders_own_records() { ex.stage = Stage::Pixel; assert_eq!(vertex_table_parameter(&ex, 9), None); } + +/// A pixel program that samples the descriptor at s8 + 0x20 with the root's +/// static sampler s0, translated against a root whose slot 1 is `kind`. +fn sampled_table_slot(kind: u32) -> ShaderExtract { + use crate::meta::{RootParam, RootRange}; + let words = [ + 0xf40c0104u32, 0xfa000020, // s[4:11] = table[s8:9] + 0x20 + 0xf09c0308, 0x00a10704, // v[7:8] = image_sample_lz(v4, s[4:11], s[20:23]) + 0xbf810000, + ]; + let isa: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let mut ex = super::super::mesh::heap_extract(&[], 0, &isa); + ex.stage = Stage::Pixel; + ex.shader_model = (6, 6); + ex.required_features = Some(0); + ex.fssm = fssm(&[], &[]); + let range = |range_type, base_register| RootRange { range_type, num: 1, base_register, space: 0, offset_in_table: u32::MAX }; + let root = ex.root_sig.as_mut().unwrap(); + root.version = 2; + root.params = vec![RootParam { visibility: 0, kind: RootParamKind::Table(vec![range(0, 0), range(kind, 7)]) }]; + let mut sampler = [0u32; 13]; + sampler[1..4].fill(3); // clamp + sampler[9] = f32::MAX.to_bits(); + root.static_samplers = vec![sampler]; + root.num_static_samplers = 1; + ex.resources = vec![PsvResource { res_type: 1, space: 0, lower: 0, upper: 0 }]; + ex +} + +#[test] +fn sampling_a_root_uav_slot_names_the_mismatched_root() { + let out = super::super::translate(&sampled_table_slot(0)).unwrap(); + assert!(out.hlsl.contains("tex7.SampleLevel(samp0"), "{}", out.hlsl); + // The program samples what that root declares as a UAV. The root was + // not the one the program's table follows. + let reason = refusal(&sampled_table_slot(1)); + assert!(reason.contains("image_sample_lz reads table UAV u7"), "{reason}"); +}