diff --git a/d3d12/crates/leonida-shader/src/emit/container.rs b/d3d12/crates/leonida-shader/src/emit/container.rs index 4f5e6dc194..df3adca4c8 100644 --- a/d3d12/crates/leonida-shader/src/emit/container.rs +++ b/d3d12/crates/leonida-shader/src/emit/container.rs @@ -238,14 +238,15 @@ pub(crate) fn psv(p: &Psv) -> Vec { let mut elements = Vec::new(); for e in p.inputs.iter().chain(p.outputs).chain(p.prims) { let name = if e.kind == 0 { + // One copy of the name per element, in element order, as DXC + // writes it (the `ISG1`/`OSG1` part above does the same): the + // validator compares this part with the one it computes from the + // module, whose name table holds a copy per element, so a shared + // copy leaves every later element with a name offset the + // module's does not have and the validator refuses the program. let needle: Vec = e.name.bytes().chain([0]).collect(); - match find(&strings, &needle) { - Some(at) => at, - None => { - strings.extend(&needle); - strings.len() - needle.len() - } - } + strings.extend(&needle); + strings.len() - needle.len() } else { 0 }; @@ -304,10 +305,6 @@ pub(crate) fn psv(p: &Psv) -> Vec { out } -fn find(hay: &[u8], needle: &[u8]) -> Option { - hay.windows(needle.len()).position(|w| w == needle) -} - /// The container: parts in order, hashed. pub(crate) fn build(parts: &[(&[u8; 4], &[u8])]) -> Vec { let table = 0x20 + 4 * parts.len(); @@ -475,6 +472,70 @@ mod tests { .is_err()); } + /// The validator compares the `PSV0` part with the one it computes from + /// the module; that name table holds a copy of a semantic's name per + /// element, so a shared copy leaves the later elements with name + /// offsets the module's table does not have and the validator refuses + /// the program. + #[test] + fn the_psv_name_table_holds_a_copy_of_a_name_per_element() { + let texcoord = SigElement { + name: "TEXCOORD".into(), + index: 0, + kind: 0, + comp: 9, + interp: 2, + row: Some(0), + col: 0, + cols: 4, + usage: 15, + half: false, + }; + let second = SigElement { + index: 1, + row: Some(2), + ..texcoord.clone() + }; + let position = SigElement { + name: "SV_Position".into(), + kind: 3, + interp: 4, + row: Some(1), + ..texcoord.clone() + }; + let psv = psv(&Psv { + stage: 1, + flag: true, + inputs: &[texcoord, second], + outputs: &[position], + prims: &[], + mesh: None, + geometry: None, + bindings: &[], + threads: [0; 3], + wave_size: None, + }); + // The name table follows the 56-byte header, the index table the + // names, and the rows the indices; each row names its element in + // its first word. + let names_len = u32::from_le_bytes(psv[56..60].try_into().unwrap()) as usize; + let names = &psv[60..60 + names_len]; + let mut at = 60 + names_len; + let indices = u32::from_le_bytes(psv[at..at + 4].try_into().unwrap()) as usize; + at += 4 + 4 * indices; + assert_eq!(u32::from_le_bytes(psv[at..at + 4].try_into().unwrap()), 16); + at += 4; + let name_of = |row: usize| { + u32::from_le_bytes(psv[at + row * 16..at + row * 16 + 4].try_into().unwrap()) as usize + }; + // The two TEXCOORDs have a copy of the name each; the system value + // has none. + assert_ne!(name_of(0), name_of(1)); + assert_eq!(&names[name_of(0)..name_of(0) + 9], b"TEXCOORD\0"); + assert_eq!(&names[name_of(1)..name_of(1) + 9], b"TEXCOORD\0"); + assert_eq!(name_of(2), 0); + } + #[test] fn md5_core_matches_the_standard() { // Standard MD5 of "abc" with standard padding, through md5_block.