diff --git a/runtime/dxbc/src/mesh_table.rs b/runtime/dxbc/src/mesh_table.rs index b607dad..522b94c 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 5d59e34..0d704af 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 69c8626..16df4bc 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 b6c35ab..f01760b 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}"); +}