diff --git a/d3d12/crates/leonida-shader/src/emit/container.rs b/d3d12/crates/leonida-shader/src/emit/container.rs index 4f5e6dc19..75f93fe35 100644 --- a/d3d12/crates/leonida-shader/src/emit/container.rs +++ b/d3d12/crates/leonida-shader/src/emit/container.rs @@ -233,19 +233,19 @@ pub(crate) fn psv(p: &Psv) -> Vec { } } // Names of arbitrary semantics (system values use ""), then indices. + // One copy of each name per element, in element order: the validator + // compares this part with the one it computes from the module, whose + // name table has a copy per element, so sharing a name between two + // elements (two `TEXCOORD`s, say) leaves the later one with an offset + // that table does not have and the program is refused. let mut strings = vec![0u8]; let mut indices: Vec = Vec::new(); let mut elements = Vec::new(); for e in p.inputs.iter().chain(p.outputs).chain(p.prims) { let name = if e.kind == 0 { 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 +304,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 +471,71 @@ mod tests { .is_err()); } + #[test] + fn the_psv_name_table_holds_a_copy_of_a_name_per_element() { + // Two arbitrary inputs share the semantic name `TEXCOORD`, as a + // vertex shader's TEXCOORD0 and TEXCOORD1 do. DXC writes a copy of + // the name for each element; the validator rebuilds the PSV from the + // module and compares the parts, so one shared copy is rejected on + // every validator before 1.8 (`Pipeline State Validation does not + // match expected for module`). + 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(1), + ..texcoord.clone() + }; + let position = SigElement { + name: "SV_Position".into(), + kind: 3, + interp: 4, + row: Some(2), + ..texcoord.clone() + }; + let psv = psv(&Psv { + stage: 1, + flag: true, + inputs: &[texcoord, second, position], + outputs: &[], + prims: &[], + mesh: None, + geometry: None, + bindings: &[], + threads: [0; 3], + wave_size: None, + }); + let word = |at: usize| u32::from_le_bytes(psv[at..at + 4].try_into().unwrap()) as usize; + let mut at = 4 + word(0); // info after its size + at += 4; // binding count (none) + let names_len = word(at); + at += 4; + let names = &psv[at..at + names_len]; + at += names_len; + let indices = word(at); + at += 4 + 4 * indices; + assert_eq!(word(at), 16, "element stride"); + at += 4; + let name_of = |element: usize| word(at + 16 * element); + // Each arbitrary element names itself at its own offset; the system + // value has none (offset 0, the empty string). + let (first, second) = (name_of(0), name_of(1)); + assert_ne!(first, second, "the TEXCOORDs share a name offset"); + assert_eq!(&names[first..first + 9], b"TEXCOORD\0"); + assert_eq!(&names[second..second + 9], b"TEXCOORD\0"); + assert_eq!(name_of(2), 0, "the system value names the empty string"); + } + #[test] fn md5_core_matches_the_standard() { // Standard MD5 of "abc" with standard padding, through md5_block.