From 6bf3305aedcbb6a342e034c9791a7fbea2f06bda Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:56:44 -0500 Subject: [PATCH] Rect lists: the CPU expansion finds the rect's corner, not always v1 draw_rects made every rect's fourth corner as v0 + v2 - v1, which is right when the rect's corner (the vertex between the other two) is v1. Rects are also sent with it at v0 - (x1,y1) (x1,y2) (x2,y1), as Mesa's r600 and radeonsi blits do - and then the fourth vertex landed outside the rect, at (x2, 2*y1 - y2). The corner is now found from the vertices (the one that differs alone in the fewest words; v1 when there is no single one, as before), and the fourth corner is a + b - corner. Both triangles start with v0 and keep the winding of v0 v1 v2. For a rect with its corner at v1 the six vertices are the ones made before. A rect's vertices in several vertex buffers get one corner, and every slot's six vertices use it. Each slot on its own could find another: an attribute with no word two vertices share falls back to v1, and paired a position with another corner's attribute (review by shdann). The corner is found from the positions alone where the pipeline's input layout names them: an element POSITION or SV_Position of two to four floats, noted when a translated rect list pipeline is made. With every word of the vertex voting an attribute could outvote the position: with (0,0) (1,0) (0,1) and a float2 of (1,1) (.5,.5) (.5,.5) v0 differs alone in two words, the corner was taken as v1 and the fourth position came out at (-1,1) instead of (1,1) (review by Alpha325). A layout that does not name the positions is still expanded from the words of all slots, as if they were one interleaved vertex. Assisted-by: Claude Code (Claude Fable 5.2) Co-Authored-By: Claude Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/xcl/src/hle/d3d12x/bind.rs | 237 +++++++++++++++++++++++++++--- crates/xcl/src/hle/d3d12x/real.rs | 79 ++++++++++ 2 files changed, 294 insertions(+), 22 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index fad934add..451afc210 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -402,11 +402,9 @@ unsafe fn argument_order_buffer(list: &ID3D12GraphicsCommandList) -> Option bool { if !cpu_rect_expansion(with(this, |s| s.pso)) { return false; @@ -416,7 +414,8 @@ unsafe fn draw_rects(this: *mut c_void, list: &ID3D12GraphicsCommandList, vertic if vbs.is_empty() || rects == 0 || rects > 4096 { return false; } - let mut views = Vec::new(); + // (slot, the title's first vertex, stride, the six vertices a rect made here) + let mut streams = Vec::new(); for &(slot, (va, bytes, stride), _) in &vbs { let (s, at) = (stride as usize, va as usize + first_vertex as usize * stride as usize); let len = rects * 3 * s; @@ -427,24 +426,26 @@ unsafe fn draw_rects(this: *mut c_void, list: &ID3D12GraphicsCommandList, vertic if p.is_null() { return false; } - let word = |v: usize, w: usize| (at as *const u8).add(v * s + w * 4).cast::().read_unaligned(); - for r in 0..rects { - let out = p.add(r * 6 * s); - let src = (at as *const u8).add(r * 3 * s); - // v0 v1 v2, then v0 v2 v3 - std::ptr::copy_nonoverlapping(src, out, 3 * s); - std::ptr::copy_nonoverlapping(src, out.add(3 * s), s); - std::ptr::copy_nonoverlapping(src.add(2 * s), out.add(4 * s), s); - for w in 0..s / 4 { - let (a, b, c) = (word(r * 3, w), word(r * 3 + 1, w), word(r * 3 + 2, w)); - let d = if a == b && b == c { a } else { (f32::from_bits(a) + f32::from_bits(c) - f32::from_bits(b)).to_bits() }; - out.add(5 * s + w * 4).cast::().write_unaligned(d); - } + streams.push((slot, at, s, p)); + } + let strides: Vec = streams.iter().map(|x| x.2).collect(); + // (the stream with the positions, their byte offset in a vertex, bytes) + let position = super::real::rect_position(super::pso_async::resolve(with(this, |s| s.pso))) + .and_then(|(slot, at, n)| Some((streams.iter().position(|x| x.0 == slot)?, at, n))) + .filter(|&(i, at, n)| at + n <= strides[i]); + // (a copy of each rect: the title may be writing its vertices) + let mut rect: Vec> = strides.iter().map(|&s| vec![0u8; 3 * s]).collect(); + for r in 0..rects { + for (copy, &(_, at, s, _)) in rect.iter_mut().zip(&streams) { + std::ptr::copy_nonoverlapping((at as *const u8).add(r * 3 * s), copy.as_mut_ptr(), 3 * s); } - views.push((slot, D3D12_VERTEX_BUFFER_VIEW { BufferLocation: super::list_x::pc_va_of(p as u64), SizeInBytes: (rects * 6 * s) as u32, StrideInBytes: stride })); + let v: Vec<&[u8]> = rect.iter().map(|c| c.as_slice()).collect(); + let mut out: Vec<&mut [u8]> = streams.iter().map(|&(_, _, s, p)| std::slice::from_raw_parts_mut(p.add(r * 6 * s), 6 * s)).collect(); + expand_rect_streams(&v, &strides, position, &mut out); } - for (slot, v) in &views { - list.IASetVertexBuffers(*slot, Some(&[*v])); + for &(slot, _, s, p) in &streams { + let v = D3D12_VERTEX_BUFFER_VIEW { BufferLocation: super::list_x::pc_va_of(p as u64), SizeInBytes: (rects * 6 * s) as u32, StrideInBytes: s as u32 }; + list.IASetVertexBuffers(slot, Some(&[v])); } list.DrawInstanced(rects as u32 * 6, instances, 0, first_instance); for &(slot, _, pc) in &vbs { @@ -459,6 +460,99 @@ unsafe fn draw_rects(this: *mut c_void, list: &ID3D12GraphicsCommandList, vertic true } +/// The corner of a rect list's rect – the vertex between the other two – from its three +/// vertices (`v`, `s` bytes each). It is not always the same vertex: (x1,y1) (x1,y2) (x2,y1) +/// has it at v0 (Mesa's r600 and radeonsi blits), (x1,y1) (x1,y2) (x2,y2) at v1 +/// (xf86-video-ati's r600 EXA; a retail title's full-screen passes: (−1,1) (−1,−1) (1,−1)). +/// A word two of the vertices share (an x or a y, and what goes with it – a texture +/// coordinate) differs in the third, the far end of one of the corner's edges: the corner is +/// the vertex that differs alone in the fewest words. No single such vertex (not three corners +/// of a screen-aligned rect): v1. +/// +/// `v` is the positions where the input layout names them (`expand_rect_streams`). Without +/// that it is the whole vertex, and then an attribute can outvote the position: with +/// (0,0) (1,0) (0,1) and a float2 of (1,1) (½,½) (½,½), 1 − x/2 − y/2, v0 differs alone in +/// two words, v1 and v2 in one each, and the corner is taken as v1 (found by Alpha325, PR #266). +fn rect_corner(v: &[u8], s: usize) -> usize { + let word = |i: usize, w: usize| u32::from_le_bytes(v[i * s + w * 4..][..4].try_into().unwrap()); + let mut alone = [0u32; 3]; + for w in 0..s / 4 { + let (a, b, c) = (word(0, w), word(1, w), word(2, w)); + if a == b && b != c { + alone[2] += 1; + } else if a == c && a != b { + alone[1] += 1; + } else if b == c && a != b { + alone[0] += 1; + } + } + let least = alone.into_iter().min().unwrap_or(0); + match alone.iter().filter(|&&n| n == least).count() { + 1 => alone.iter().position(|&n| n == least).unwrap_or(1), + _ => 1, + } +} + +/// One rect of a rect list whose vertices come from several vertex buffers (`v[i]`: the rect's +/// three vertices in slot i, `s[i]` bytes each; `out[i]`: its six). One corner for the rect: +/// every slot's six vertices are then the same corners in the same order. (Each slot on its +/// own could find another – an attribute with no word two vertices share falls back to v1 – +/// and pair a position with another corner's attribute.) It is found from the positions +/// (`position`: their index in `v`, byte offset in a vertex and bytes – `real::rect_position`) +/// or, where the input layout does not name them, from the words of all slots as if they were +/// one interleaved vertex. +fn expand_rect_streams(v: &[&[u8]], s: &[usize], position: Option<(usize, usize, usize)>, out: &mut [&mut [u8]]) { + let mut all = Vec::with_capacity(3 * s.iter().sum::()); + for i in 0..3 { + match position { + Some((p, at, n)) => all.extend_from_slice(&v[p][i * s[p] + at..][..n]), + None => { + for (vi, &si) in v.iter().zip(s) { + all.extend_from_slice(&vi[i * si..][..si]); + } + } + } + } + let k = rect_corner(&all, all.len() / 3); + for ((vi, &si), o) in v.iter().zip(s).zip(out.iter_mut()) { + expand_rect(vi, si, k, o); + } +} + +/// One rect of a rect list (`v`: its three vertices, `s` bytes each) as two triangles (`out`: +/// six vertices). The fourth corner is the one opposite the rect's corner (`k`, `rect_corner`): +/// a + b − corner of the other two vertices a and b, each 4-byte word that is not the same in +/// all three vertices (those are kept – a packed colour) taken as a float. Both triangles +/// start with v0 and wind as v0 v1 v2 does: what a pixel shader reads flat (on the PC a +/// triangle's first vertex's value) is v0's in both halves. +fn expand_rect(v: &[u8], s: usize, k: usize, out: &mut [u8]) { + let word = |i: usize, w: usize| u32::from_le_bytes(v[i * s + w * 4..][..4].try_into().unwrap()); + let fourth = |w: usize| { + let (c, a, b) = (word(k, w), word((k + 1) % 3, w), word((k + 2) % 3, w)); + if c == a && a == b { + c + } else { + (f32::from_bits(a) + f32::from_bits(b) - f32::from_bits(c)).to_bits() + } + }; + // 3: the fourth corner d. (corner, a, b) is v0 v1 v2 in its order or rotated, and (corner, + // a, d), (corner, d, b) and (a, d, b) wind as it does + let order: [usize; 6] = match k { + 0 => [0, 1, 3, 0, 3, 2], + 1 => [0, 1, 2, 0, 2, 3], + _ => [0, 1, 2, 0, 3, 1], + }; + for (to, from) in out.chunks_exact_mut(s).zip(order) { + if from < 3 { + to.copy_from_slice(&v[from * s..][..s]); + } else { + for (w, word) in to.chunks_exact_mut(4).enumerate() { + word.copy_from_slice(&fourth(w).to_le_bytes()); + } + } + } +} + /// Whether a rect list drawn with the bound pipeline `pso` is expanded on the CPU (`draw_rects`): /// not with the rect list GS (`real::has_rect_list_gs`), which completes each rect itself – both /// would draw every rect twice (each CPU half is three corners of the rect). @@ -1223,6 +1317,105 @@ void main(uint t : SV_GroupIndex) { } } + /// `expand_rect` on vertices of position (x, y, 0, 1), texture coordinate (u, v) and a packed + /// colour: the six vertices' positions and texture coordinates, and whether every vertex kept + /// the position's z and w and the colour. + fn expanded(corners: [[f32; 2]; 3], uv: impl Fn([f32; 2]) -> [f32; 2]) -> (Vec<([f32; 2], [f32; 2])>, bool) { + const STRIDE: usize = 28; + let mut v = Vec::new(); + for p in corners { + let t = uv(p); + for f in [p[0], p[1], 0.0, 1.0, t[0], t[1]] { + v.extend_from_slice(&f.to_le_bytes()); + } + v.extend_from_slice(&0xFF33_6699u32.to_le_bytes()); + } + let mut out = vec![0u8; 6 * STRIDE]; + expand_rect_streams(&[&v], &[STRIDE], None, &mut [&mut out]); + let f = |b: &[u8], w: usize| f32::from_le_bytes(b[w * 4..][..4].try_into().unwrap()); + let kept = out.chunks_exact(STRIDE).all(|b| f(b, 2) == 0.0 && f(b, 3) == 1.0 && b[24..] == 0xFF33_6699u32.to_le_bytes()); + (out.chunks_exact(STRIDE).map(|b| ([f(b, 0), f(b, 1)], [f(b, 4), f(b, 5)])).collect(), kept) + } + + /// A rect list's rect on the CPU, for each vertex its corner can be: (x1,y1) (x1,y2) (x2,y1) + /// – v0, as Mesa's r600 and radeonsi blits send it –, (x1,y1) (x1,y2) (x2,y2) – v1, as + /// xf86-video-ati's r600 EXA does –, and v2. The two triangles are the rect's halves along + /// one diagonal, wind as v0 v1 v2 does and start with v0; the fourth corner continues the + /// texture coordinate. Synthetic: the rect is the expectation, not a measurement of hardware. + #[test] + fn rects_expanded_on_the_cpu_have_the_corner_opposite_their_own() { + let (x1, x2, y1, y2) = (16.0f32, 48.0, 8.0, 24.0); + let uv = |[x, y]: [f32; 2]| [(x - x1) / (x2 - x1), (y - y1) / (y2 - y1)]; + let area = |t: &[([f32; 2], [f32; 2])]| (t[1].0[0] - t[0].0[0]) * (t[2].0[1] - t[0].0[1]) - (t[2].0[0] - t[0].0[0]) * (t[1].0[1] - t[0].0[1]); + for (name, corners) in [ + ("corner v0", [[x1, y1], [x1, y2], [x2, y1]]), + ("corner v1", [[x1, y1], [x1, y2], [x2, y2]]), + ("corner v1, the other way round", [[x1, y2], [x1, y1], [x2, y1]]), + ("corner v2", [[x1, y2], [x2, y1], [x1, y1]]), + ] { + let (v, kept) = expanded(corners, uv); + let pos: Vec<[f32; 2]> = v.iter().map(|p| p.0).collect(); + assert!(kept, "{name}: z, w and the colour are kept"); + for corner in [[x1, y1], [x1, y2], [x2, y1], [x2, y2]] { + assert!(pos.contains(&corner), "{name}: no vertex at {corner:?} in {pos:?}"); + } + assert!(v.iter().all(|&(p, t)| t == uv(p)), "{name}: texture coordinates {v:?}"); + // each triangle is half the rect, in the winding of the title's three vertices + let first = area(&[(corners[0], [0.0; 2]), (corners[1], [0.0; 2]), (corners[2], [0.0; 2])]); + assert_eq!(first.abs(), (x2 - x1) * (y2 - y1)); + assert_eq!([area(&v[..3]), area(&v[3..])], [first, first], "{name}: {pos:?}"); + // … on either side of a diagonal, and both start with v0 + assert_eq!(pos[..3].iter().filter(|p| pos[3..].contains(p)).count(), 2, "{name}: {pos:?}"); + assert_eq!([pos[0], pos[3]], [corners[0], corners[0]], "{name}: {pos:?}"); + } + // not three corners of a screen-aligned rect: as before, v0 + v2 − v1 + let (v, _) = expanded([[0.0, 0.0], [8.0, 2.0], [10.0, 9.0]], |p| p); + assert_eq!(v.iter().map(|p| p.0).collect::>(), [[0.0, 0.0], [8.0, 2.0], [10.0, 9.0], [0.0, 0.0], [10.0, 9.0], [2.0, 7.0]]); + } + + /// A rect whose position and another attribute come from two vertex buffers (review of + /// PR #266 by shdann): one corner for the rect, so the two buffers' six vertices are the same + /// corners in the same order. The positions (0,0) (1,0) (0,1) have their corner at v0; the + /// attribute 10 + 10x + 20y (10, 20, 30) has no word two vertices share and on its own falls + /// back to v1 – it put 30 at the fourth corner (1,1), not 40. + #[test] + fn a_rect_split_over_vertex_buffers_has_one_corner() { + let bytes = |x: &[f32]| x.iter().flat_map(|f| f.to_le_bytes()).collect::>(); + let (pos, attr) = (bytes(&[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]), bytes(&[10.0, 20.0, 30.0])); + let (mut out_pos, mut out_attr) = (vec![0u8; 6 * 8], vec![0u8; 6 * 4]); + expand_rect_streams(&[&pos, &attr], &[8, 4], None, &mut [&mut out_pos, &mut out_attr]); + let f = |b: &[u8], w: usize| f32::from_le_bytes(b[w * 4..][..4].try_into().unwrap()); + let v: Vec<(f32, f32, f32)> = (0..6).map(|i| (f(&out_pos, 2 * i), f(&out_pos, 2 * i + 1), f(&out_attr, i))).collect(); + assert!(v.iter().any(|&(x, y, _)| (x, y) == (1.0, 1.0)), "no fourth corner: {v:?}"); + assert!(v.iter().all(|&(x, y, a)| a == 10.0 + 10.0 * x + 20.0 * y), "{v:?}"); + } + + /// The corner comes from the positions alone where the input layout names them (review of + /// PR #266 by Alpha325): with the positions (0,0) (1,0) (0,1) – corner v0 – the float2 + /// attribute 1 − x/2 − y/2 in both words, (1,1) (½,½) (½,½), differs alone at v0 twice. + /// With every word voting that made v1 the corner and the fourth position (−1,1), not (1,1) – + /// which is still what a layout that does not name the positions gets. In two vertex + /// buffers, and in one with the attribute first. + #[test] + fn a_rects_corner_is_found_from_its_positions() { + let bytes = |x: &[f32]| x.iter().flat_map(|f| f.to_le_bytes()).collect::>(); + let f = |b: &[u8], w: usize| f32::from_le_bytes(b[w * 4..][..4].try_into().unwrap()); + let (pos, attr) = (bytes(&[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]), bytes(&[1.0, 1.0, 0.5, 0.5, 0.5, 0.5])); + let one: Vec = (0..3).flat_map(|i| [&attr[i * 8..][..8], &pos[i * 8..][..8]].concat()).collect(); + let fourth = |split: Option<(usize, usize, usize)>, interleaved: Option<(usize, usize, usize)>| { + let (mut out_pos, mut out_attr, mut out) = (vec![0u8; 6 * 8], vec![0u8; 6 * 8], vec![0u8; 6 * 16]); + expand_rect_streams(&[&attr, &pos], &[8, 8], split, &mut [&mut out_attr, &mut out_pos]); + expand_rect_streams(&[&one], &[16], interleaved, &mut [&mut out]); + let v: Vec<[f32; 4]> = (0..6).map(|i| [f(&out_pos, 2 * i), f(&out_pos, 2 * i + 1), f(&out_attr, 2 * i), f(&out_attr, 2 * i + 1)]).collect(); + assert_eq!(v, (0..6).map(|i| [f(&out, 4 * i + 2), f(&out, 4 * i + 3), f(&out, 4 * i), f(&out, 4 * i + 1)]).collect::>()); + assert!(v.iter().all(|&[x, y, a, b]| a == 1.0 - x / 2.0 - y / 2.0 && b == a), "{v:?}"); + // (the one vertex that is none of the title's three) + v.iter().map(|p| [p[0], p[1]]).find(|p| !pos.chunks_exact(8).any(|c| [f(c, 0), f(c, 1)] == *p)) + }; + assert_eq!(fourth(Some((1, 0, 8)), Some((0, 8, 8))), Some([1.0, 1.0])); + assert_eq!(fourth(None, None), Some([-1.0, 1.0])); + } + /// Minimal RTS0 v2 root signature blob with one 4-constant parameter. fn test_rts0_blob() -> Vec { let words: [u32; 12] = [ diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 4ea637488..a04115b12 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1761,6 +1761,50 @@ fn note_rect_list_gs(pso: usize, copy: &D3D12_GRAPHICS_PIPELINE_STATE_DESC, rect } } +/// translated rect list pipelines → where their input layout has the position (`layout_position`) +static RECT_POSITION: Mutex>> = Mutex::new(None); + +/// Where the vertices of the rect list pipeline `pso` (a stand-in resolved) have their position: +/// (slot, byte offset, bytes). None: its input layout does not say (`layout_position`). +pub fn rect_position(pso: usize) -> Option<(u32, usize, usize)> { + RECT_POSITION.lock().unwrap().as_ref()?.get(&pso).copied() +} + +/// Where an input layout has the vertex position, for the CPU expansion of rect lists +/// (`bind::draw_rects`): the (slot, byte offset, bytes) of the element named POSITION or +/// SV_Position (index 0) – two to four floats, read per vertex. The name is the title's +/// convention: nothing makes a vertex shader compute SV_Position from that input, and which +/// input it does compute it from is not looked for in its code. None: no such element, another +/// format, or one appended (D3D12_APPEND_ALIGNED_ELEMENT) to elements of its slot. +unsafe fn layout_position(elems: &[D3D12_INPUT_ELEMENT_DESC]) -> Option<(u32, usize, usize)> { + use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT, DXGI_FORMAT_R32G32_FLOAT}; + let named = |e: &D3D12_INPUT_ELEMENT_DESC, n: &[u8]| !e.SemanticName.is_null() && e.SemanticName.as_bytes().eq_ignore_ascii_case(n); + let k = elems.iter().position(|e| e.SemanticIndex == 0 && (named(e, b"POSITION") || named(e, b"SV_POSITION")))?; + let e = &elems[k]; + let bytes = match e.Format { + DXGI_FORMAT_R32G32_FLOAT => 8, + DXGI_FORMAT_R32G32B32_FLOAT => 12, + DXGI_FORMAT_R32G32B32A32_FLOAT => 16, + _ => return None, + }; + let appended = e.AlignedByteOffset == D3D12_APPEND_ALIGNED_ELEMENT; + if e.InputSlotClass != D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA || (appended && elems[..k].iter().any(|p| p.InputSlot == e.InputSlot)) { + return None; + } + Some((e.InputSlot, if appended { 0 } else { e.AlignedByteOffset as usize }, bytes)) +} + +/// Note where the rect list pipeline `pso`, made from the title's `d`, has its position. +unsafe fn note_rect_position(pso: usize, d: &D3D12_GRAPHICS_PIPELINE_STATE_DESC) { + let il = &d.InputLayout; + if pso == 0 || d.PrimitiveTopologyType.0 != RECT_LIST_TYPE || il.NumElements == 0 || il.pInputElementDescs.is_null() { + return; + } + if let Some(p) = layout_position(std::slice::from_raw_parts(il.pInputElementDescs, il.NumElements as usize)) { + RECT_POSITION.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso, p); + } +} + /// Whether `pso` runs a translated console compute shader. pub fn is_translated_compute(pso: usize) -> bool { pso != 0 && TRANSLATED_COMPUTE.lock().unwrap().as_ref().is_some_and(|s| s.contains(&pso)) @@ -1830,6 +1874,9 @@ fn forget_pso(pso: usize) { if let Some(s) = RECT_LIST_GS.lock().unwrap().as_mut() { s.remove(&pso); } + if let Some(m) = RECT_POSITION.lock().unwrap().as_mut() { + m.remove(&pso); + } if let Some(m) = PSO_RS.lock().unwrap().as_mut() { m.remove(&pso); } @@ -5186,6 +5233,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const super::dred::label(*ppv as usize, &[("vs", Some(vs)), ("ps", shader_bytes(&d.PS))]); TRANSLATED_GRAPHICS.lock().unwrap().get_or_insert_with(HashSet::new).insert(*ppv as usize); note_rect_list_gs(*ppv as usize, ©, rect_gs.is_some()); + note_rect_position(*ppv as usize, d); if super::present::trace_frame() { let ps = shader_bytes(&d.PS).map_or(0, crate::gpu::shader::fnv64); PSO_SHADERS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (crate::gpu::shader::fnv64(vs), ps)); @@ -7329,6 +7377,37 @@ mod rect_list_pso_tests { assert!(!has_rect_list_gs(kept)); } + /// `rect_position`: the POSITION element of a rect list pipeline's input layout – its slot, + /// offset and bytes –, until the pipeline's address is forgotten; not for a layout without + /// one, a position that is not floats or is read per instance, or another topology type. + #[test] + fn rect_list_pipelines_know_where_their_layout_has_the_position() { + use windows::Win32::Graphics::Dxgi::Common::*; + let el = |name: PCSTR, format, slot, at, class| D3D12_INPUT_ELEMENT_DESC { SemanticName: name, Format: format, InputSlot: slot, AlignedByteOffset: at, InputSlotClass: class, ..Default::default() }; + let (vertex, instance) = (D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA); + let (uv, pos) = (el(windows::core::s!("TEXCOORD"), DXGI_FORMAT_R32G32_FLOAT, 0, 0, vertex), el(windows::core::s!("Position"), DXGI_FORMAT_R32G32B32_FLOAT, 1, 4, vertex)); + let desc = |elems: &[D3D12_INPUT_ELEMENT_DESC], ty| D3D12_GRAPHICS_PIPELINE_STATE_DESC { + InputLayout: D3D12_INPUT_LAYOUT_DESC { pInputElementDescs: elems.as_ptr(), NumElements: elems.len() as u32 }, + PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE(ty), + ..Default::default() + }; + let (rects, triangles, unnamed) = (0x7E57_6000usize, 0x7E57_7000usize, 0x7E57_8000usize); + unsafe { + note_rect_position(rects, &desc(&[uv, pos], RECT_LIST_TYPE)); + note_rect_position(triangles, &desc(&[uv, pos], 3)); + note_rect_position(unnamed, &desc(&[uv], RECT_LIST_TYPE)); + assert_eq!((rect_position(rects), rect_position(triangles), rect_position(unnamed), rect_position(0)), (Some((1, 4, 12)), None, None, None)); + let sv = |format, at, class| el(windows::core::s!("SV_Position"), format, 0, at, class); + assert_eq!(layout_position(&[sv(DXGI_FORMAT_R32G32B32A32_FLOAT, D3D12_APPEND_ALIGNED_ELEMENT, vertex), uv]), Some((0, 0, 16))); + assert_eq!(layout_position(&[uv, sv(DXGI_FORMAT_R32G32_FLOAT, D3D12_APPEND_ALIGNED_ELEMENT, vertex)]), None); + assert_eq!(layout_position(&[sv(DXGI_FORMAT_R16G16_FLOAT, 0, vertex)]), None); + assert_eq!(layout_position(&[sv(DXGI_FORMAT_R32G32_FLOAT, 0, instance)]), None); + assert_eq!(layout_position(&[D3D12_INPUT_ELEMENT_DESC { SemanticIndex: 1, ..pos }]), None); + } + forget_pso(rects); + assert_eq!(rect_position(rects), None); + } + /// An untranslated rect list pipeline from a stream gets the PC's topology type (TRIANGLE), /// as at 956a476; other types, and a direct CreateGraphicsPipelineState, are left as they are. #[test]