Shader: rectangles complete opposite their right angle, wherever it is
A console rectangle list gives three corners per rectangle and the hardware adds the fourth opposite the right angle. The geometry shader that completes them on the host (and the CPU expansion used without it) took the right angle to be at v1, as the blits give it (top left, bottom left, bottom right). The tiled lighting's vertex shader (VS_TiledLighting, 3 vertices per tile: corner c = vertex % 3 at x + bit 1, y + bit 0) gives top left, bottom left, top right, the right angle at v0, so every lighting tile drew its upper-left half and the lower-right half of the tile above: tiles whose neighbour below was in another list lost their lower-right half and lit the one above, the right-triangle wedges along silhouettes and kerbs of runs shards22 (frames 1603, 1615, 1627; their depth is clean there). Now the right angle is the corner opposite the longest side (`emit::right_angle`, ties and NaN keep v1), the fourth corner is va + vb - vc and the second triangle (va, vb, v3), turning as the first for any three points; the CPU expansion takes the same rule from the first float element of two or more components. Tests: emit::rect::rule_tests::right_angles_are_found_in_any_order; emit::rect::tests::rectangles_get_their_fourth_corner draws the three orders (and three points on a line, which draw nothing) on hardware and WARP, and fails with the old rule; primitive::tests:: rectangles_complete_opposite_their_right_angle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec9e750eb7
commit
8ff7913516
3 files changed
+179
-63
No files matched your search
@@ -1,9 +1,8 @@
|
||||
//! Console primitive types the host lacks. A rectangle list gives three
|
||||
//! corners per rectangle, the second the one between the other two, and
|
||||
//! the hardware adds the fourth at `v0 + v2 - v1` (Grand Theft Auto VI's
|
||||
//! full-screen rectangles come as top left, bottom left, bottom right); on
|
||||
//! the host each rectangle becomes two triangles, with the fourth vertex
|
||||
//! made from the other three on the CPU.
|
||||
//! corners per rectangle, in any order, and the hardware adds the fourth
|
||||
//! opposite the right angle (`victory_shader::emit::right_angle`); on the
|
||||
//! host each rectangle becomes two triangles, with the fourth vertex made
|
||||
//! from the other three on the CPU when no geometry shader does it.
|
||||
//!
|
||||
//! shortcut: the fourth vertex is computed from vertex inputs, not from
|
||||
//! vertex shader outputs as the hardware does; that is the same for vertex
|
||||
@@ -13,8 +12,11 @@
|
||||
//! next one, which the layout allows).
|
||||
|
||||
/// Expands `rects` rectangles of `stride`-byte vertices from `src` into six
|
||||
/// vertices each in `dst`: `(v0, v1, v2)` and `(v0, v2, v3)`, so both
|
||||
/// triangles turn the same way. `elements` are (byte offset, format).
|
||||
/// vertices each in `dst`: `(v0, v1, v2)` and `(va, vb, v3)`, so both
|
||||
/// triangles turn the same way, with the right angle `vc` picked from the
|
||||
/// first float element of two or more components (taken as the position;
|
||||
/// none: at `v1`), `va` before it, `vb` after it and `v3 = va + vb - vc`.
|
||||
/// `elements` are (byte offset, format).
|
||||
pub(crate) fn expand_rects(src: &[u8], dst: &mut [u8], stride: usize, elements: &[(u32, u32)]) {
|
||||
if stride == 0 {
|
||||
return;
|
||||
@@ -27,11 +29,23 @@ pub(crate) fn expand_rects(src: &[u8], dst: &mut [u8], stride: usize, elements:
|
||||
.zip(dst.chunks_exact_mut(out_rect))
|
||||
{
|
||||
let v = |i: usize| &rect[i * stride..(i + 1) * stride];
|
||||
for (slot, i) in [0, 1, 2, 0, 2].into_iter().enumerate() {
|
||||
let right = elements
|
||||
.iter()
|
||||
.find_map(|&(offset, format)| {
|
||||
let at = offset as usize;
|
||||
let (n, kind) = components(format)?;
|
||||
let float = matches!(kind, Kind::F32 | Kind::F16);
|
||||
(float && n >= 2 && stride.saturating_sub(at) / kind.bytes() >= 2).then(|| {
|
||||
victory_shader::emit::right_angle([0, 1, 2].map(|i| [0, 1].map(|k| kind.read(&v(i)[at..], k))))
|
||||
})
|
||||
})
|
||||
.unwrap_or(1);
|
||||
let (a, b) = ((right + 2) % 3, (right + 1) % 3);
|
||||
for (slot, i) in [0, 1, 2, a, b].into_iter().enumerate() {
|
||||
out[slot * stride..(slot + 1) * stride].copy_from_slice(v(i));
|
||||
}
|
||||
let fourth = &mut out[5 * stride..];
|
||||
fourth.copy_from_slice(v(0));
|
||||
fourth.copy_from_slice(v(a));
|
||||
for &(offset, format) in elements {
|
||||
let at = offset as usize;
|
||||
let Some((n, kind)) = components(format) else {
|
||||
@@ -42,7 +56,7 @@ pub(crate) fn expand_rects(src: &[u8], dst: &mut [u8], stride: usize, elements:
|
||||
let inside = stride.saturating_sub(at) / kind.bytes();
|
||||
for c in 0..n.min(inside) {
|
||||
let get = |i: usize| kind.read(&v(i)[at..], c);
|
||||
let value = get(0) + get(2) - get(1);
|
||||
let value = get(a) + get(b) - get(right);
|
||||
kind.write(&mut fourth[at..], c, value);
|
||||
}
|
||||
}
|
||||
@@ -203,6 +217,30 @@ mod tests {
|
||||
assert_eq!(half_to_f32(0xc000), -2.0);
|
||||
}
|
||||
|
||||
/// The tiled lighting's order (top left, bottom left, top right: the
|
||||
/// right angle first) completes at the bottom right, the second
|
||||
/// triangle (v2, v1, v3); a triangle that is no rectangle's keeps its
|
||||
/// winding in both halves.
|
||||
#[test]
|
||||
fn rectangles_complete_opposite_their_right_angle() {
|
||||
let vertex = |x: f32, y: f32| [x.to_le_bytes(), y.to_le_bytes()].concat();
|
||||
let src = [vertex(0.0, 0.0), vertex(0.0, 1.0), vertex(1.0, 0.0)].concat();
|
||||
let mut dst = vec![0u8; 6 * 8];
|
||||
expand_rects(&src, &mut dst, 8, &[(0, 16)]);
|
||||
assert_eq!(dst[24..], [&src[16..24], &src[8..16], &vertex(1.0, 1.0)[..]].concat()[..]);
|
||||
let read = |d: &[u8], i: usize| {
|
||||
let f = |k: usize| f32::from_le_bytes(d[i * 8 + k * 4..i * 8 + k * 4 + 4].try_into().unwrap());
|
||||
[f(0), f(1)]
|
||||
};
|
||||
let turn = |d: &[u8], i: usize| {
|
||||
let [a, b, c] = [i, i + 1, i + 2].map(|k| read(d, k));
|
||||
(b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
|
||||
};
|
||||
let src = [vertex(0.0, 0.0), vertex(3.0, 1.0), vertex(1.0, 0.0)].concat();
|
||||
expand_rects(&src, &mut dst, 8, &[(0, 16)]);
|
||||
assert!(turn(&dst, 0) * turn(&dst, 3) > 0.0, "same winding");
|
||||
}
|
||||
|
||||
/// A rounding that carries out of the mantissa raises the exponent
|
||||
/// (the carry used to be or-ed into an odd exponent, halving the value).
|
||||
#[test]
|
||||
|
||||
@@ -81,7 +81,8 @@
|
||||
//! loads and per-lane rows), so tables are never copied per draw.
|
||||
//! * **Rectangle lists** ([`rectangle_gs`]): a geometry shader made from a
|
||||
//! vertex shader's outputs completes each rectangle with its fourth
|
||||
//! corner `v0 + v2 - v1`, as the console's hardware does.
|
||||
//! corner opposite the right angle ([`right_angle`]), as the console's
|
||||
//! hardware does.
|
||||
//! * **Heap indices** (added 2026-11-24) are clamped to [`HEAP_VIEWS`] − 1
|
||||
//! (samplers [`HEAP_SAMPLERS`] − 1): the host's heaps have at least that
|
||||
//! many slots, those past its own null, so an index past the heap (data
|
||||
@@ -206,7 +207,7 @@ mod flow_tests;
|
||||
pub mod host;
|
||||
mod lower;
|
||||
mod rect;
|
||||
pub use rect::rectangle_gs;
|
||||
pub use rect::{rectangle_gs, right_angle};
|
||||
#[cfg(all(test, windows))]
|
||||
mod memory_tests;
|
||||
mod sampler;
|
||||
|
||||
@@ -1,16 +1,37 @@
|
||||
//! The console's rectangle lists on the host. A rectangle comes as three
|
||||
//! corners, the second between the other two, and the hardware adds the
|
||||
//! fourth at `v0 + v2 - v1`, for the position and every attribute. The host
|
||||
//! has no such primitive; a geometry shader made for the vertex shader that
|
||||
//! feeds it takes each triangle `(v0, v1, v2)` and emits it with its
|
||||
//! completion `(v0, v2, v3)`, both turning the same way. Integer outputs
|
||||
//! (which can only be flat) keep `v0`'s value at the fourth corner.
|
||||
//! corners in any order (the blits put the right angle at `v1`, the tiled
|
||||
//! lighting at `v0`), and the hardware adds the fourth opposite the right
|
||||
//! angle `vc`, at `va + vb - vc`, for the position and every attribute. The
|
||||
//! host has no such primitive; a geometry shader made for the vertex shader
|
||||
//! that feeds it takes each triangle `(v0, v1, v2)`, finds the right angle
|
||||
//! opposite its longest side, and emits the triangle with its completion
|
||||
//! `(va, vb, v3)` (`va` before `vc`, `vb` after it), both turning the same
|
||||
//! way. Integer outputs (which can only be flat) keep `va`'s value at the
|
||||
//! fourth corner.
|
||||
|
||||
use super::bitcode::{Attr, Bin, Term};
|
||||
use super::bitcode::{Attr, Bin, Term, V, pred};
|
||||
use super::shader::Shader;
|
||||
use crate::ShaderError;
|
||||
use crate::dxbc::{Container, FourCC};
|
||||
|
||||
/// Which of a rectangle's three corners (x and y of each) holds the right
|
||||
/// angle: the one opposite the longest side, else (ties, NaN) `1`. The
|
||||
/// geometry shader of [`rectangle_gs`] makes the same choice on the GPU.
|
||||
pub fn right_angle(xy: [[f32; 2]; 3]) -> usize {
|
||||
let side = |i: usize, j: usize| {
|
||||
let (dx, dy) = (xy[i][0] - xy[j][0], xy[i][1] - xy[j][1]);
|
||||
dx * dx + dy * dy
|
||||
};
|
||||
let (d12, d20, d01) = (side(1, 2), side(2, 0), side(0, 1));
|
||||
if d12 > d20 && d12 > d01 {
|
||||
0
|
||||
} else if d01 > d12 && d01 > d20 {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
/// The geometry shader that completes the rectangles of the vertex shader
|
||||
/// container `vs` (its output signature is the shader's input and output).
|
||||
pub fn rectangle_gs(vs: &[u8]) -> Result<Vec<u8>, ShaderError> {
|
||||
@@ -45,25 +66,59 @@ pub fn rectangle_gs(vs: &[u8]) -> Result<Vec<u8>, ShaderError> {
|
||||
}
|
||||
corners.push(values);
|
||||
}
|
||||
let fourth: Vec<Vec<_>> = elements
|
||||
// The right angle, as [`right_angle`] picks it from the position.
|
||||
let position = elements
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(id, e)| {
|
||||
(0..usize::from(e.cols))
|
||||
.map(|col| {
|
||||
let [v0, v1, v2] = [0, 1, 2].map(|k| corners[k][id][col]);
|
||||
if e.comp == 9 {
|
||||
let sum = s.m.bin(b, Bin::Add, v0, v2);
|
||||
s.m.bin(b, Bin::Sub, sum, v1)
|
||||
} else {
|
||||
v0
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
corners.push(fourth);
|
||||
for triangle in [[0, 1, 2], [0, 2, 3]] {
|
||||
.position(|e| e.kind == 3 && e.cols >= 2 && e.comp == 9)
|
||||
.ok_or(ShaderError::Malformed("a rectangle list without a position"))?;
|
||||
let side = |s: &mut Shader, i: usize, j: usize| {
|
||||
let mut sum = None;
|
||||
for (&p, &q) in corners[i][position].iter().zip(&corners[j][position]).take(2) {
|
||||
let d = s.m.bin(b, Bin::Sub, p, q);
|
||||
let sq = s.m.bin(b, Bin::Mul, d, d);
|
||||
sum = Some(match sum {
|
||||
Some(x) => s.m.bin(b, Bin::Add, x, sq),
|
||||
None => sq,
|
||||
});
|
||||
}
|
||||
sum.expect("two columns")
|
||||
};
|
||||
let (d12, d20, d01) = (side(&mut s, 1, 2), side(&mut s, 2, 0), side(&mut s, 0, 1));
|
||||
let longest = |s: &mut Shader, d: V, others: [V; 2]| {
|
||||
let [x, y] = others.map(|o| s.m.cmp(b, pred::OGT, d, o));
|
||||
s.m.bin(b, Bin::And, x, y)
|
||||
};
|
||||
let (at0, at2) = (longest(&mut s, d12, [d20, d01]), longest(&mut s, d01, [d12, d20]));
|
||||
// `x0` if the right angle is at v0, `x2` if at v2, else `x1`.
|
||||
let pick = |s: &mut Shader, [x0, x1, x2]: [V; 3]| {
|
||||
let x = s.m.select(b, at2, x2, x1);
|
||||
s.m.select(b, at0, x0, x)
|
||||
};
|
||||
// The completing triangle (va, vb, v3), element by element.
|
||||
let mut second: [Vec<Vec<V>>; 3] = Default::default();
|
||||
for (id, e) in elements.iter().enumerate() {
|
||||
let mut cols: [Vec<V>; 3] = Default::default();
|
||||
let [c0, c1, c2] = [0, 1, 2].map(|k| &corners[k][id]);
|
||||
for ((&v0, &v1), &v2) in c0.iter().zip(c1).zip(c2) {
|
||||
let va = pick(&mut s, [v2, v0, v1]);
|
||||
let vb = pick(&mut s, [v1, v2, v0]);
|
||||
let v3 = if e.comp == 9 {
|
||||
let vc = pick(&mut s, [v0, v1, v2]);
|
||||
let sum = s.m.bin(b, Bin::Add, va, vb);
|
||||
s.m.bin(b, Bin::Sub, sum, vc)
|
||||
} else {
|
||||
va
|
||||
};
|
||||
for (k, v) in [va, vb, v3].into_iter().enumerate() {
|
||||
cols[k].push(v);
|
||||
}
|
||||
}
|
||||
for (k, c) in cols.into_iter().enumerate() {
|
||||
second[k].push(c);
|
||||
}
|
||||
}
|
||||
corners.extend(second);
|
||||
for triangle in [[0, 1, 2], [3, 4, 5]] {
|
||||
for k in triangle {
|
||||
for (id, e) in elements.iter().enumerate() {
|
||||
let suffix = if e.comp == 9 { ".f32" } else { ".i32" };
|
||||
@@ -85,6 +140,25 @@ pub fn rectangle_gs(vs: &[u8]) -> Result<Vec<u8>, ShaderError> {
|
||||
Ok(s.finish())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod rule_tests {
|
||||
use super::right_angle;
|
||||
|
||||
/// The right angle is found wherever it is; triangles that are no
|
||||
/// rectangle's take the corner opposite their longest side (their
|
||||
/// widest angle), and ties and NaN keep the blits' corner, `v1`.
|
||||
#[test]
|
||||
fn right_angles_are_found_in_any_order() {
|
||||
let (tl, bl, br, tr) = ([0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]);
|
||||
assert_eq!(right_angle([tl, bl, br]), 1);
|
||||
assert_eq!(right_angle([tl, bl, tr]), 0);
|
||||
assert_eq!(right_angle([bl, tr, tl]), 2);
|
||||
assert_eq!(right_angle([[0.0, 0.0], [3.0, 1.0], [1.0, 0.0]]), 2, "obtuse at v2");
|
||||
assert_eq!(right_angle([tl, tl, tl]), 1, "a point");
|
||||
assert_eq!(right_angle([tl, [f32::NAN, 0.0], br]), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, windows))]
|
||||
mod tests {
|
||||
use super::super::bitcode::{Attr, Term};
|
||||
@@ -150,9 +224,12 @@ mod tests {
|
||||
s.finish()
|
||||
}
|
||||
|
||||
/// One rectangle (top left, bottom left, bottom right) with a
|
||||
/// coordinate attribute: every pixel of the square is drawn, the
|
||||
/// attribute interpolated over it, the top-right half included.
|
||||
/// One rectangle with a coordinate attribute, its right angle at each of
|
||||
/// the three corners in turn (the blits' top left, bottom left, bottom
|
||||
/// right; the tiled lighting's top left, bottom left, top right; and
|
||||
/// one with it last): every pixel of the square is drawn, the attribute
|
||||
/// interpolated over it, the missing half included. Three corners on one
|
||||
/// line draw nothing.
|
||||
#[test]
|
||||
fn rectangles_get_their_fourth_corner() {
|
||||
let (vs, ps) = (shader(false), shader(true));
|
||||
@@ -164,33 +241,33 @@ mod tests {
|
||||
v.validate(&mut c.clone()).unwrap();
|
||||
}
|
||||
}
|
||||
#[rustfmt::skip]
|
||||
let vertices = [
|
||||
-0.5, 0.5, 0.5, 1.0, -0.5, 0.5,
|
||||
-0.5, -0.5, 0.5, 1.0, -0.5, -0.5,
|
||||
0.5, -0.5, 0.5, 1.0, 0.5, -0.5,
|
||||
];
|
||||
let corner = |x: f32, y: f32| [x, y, 0.5, 1.0, x, y];
|
||||
let (tl, bl, br, tr) = (corner(-0.5, 0.5), corner(-0.5, -0.5), corner(0.5, -0.5), corner(0.5, 0.5));
|
||||
let orders = [[tl, bl, br], [tl, bl, tr], [bl, tr, tl], [tl, corner(0.0, 0.0), br]];
|
||||
for warp in [false, true] {
|
||||
let Ok(host) = Host::new(warp) else { continue };
|
||||
let root = host.root_signature_for(&[]).unwrap();
|
||||
let pso = host.pipeline_gs(&root, &vs, &gs, &ps).unwrap();
|
||||
let draw = Draw {
|
||||
roots: Vec::new(),
|
||||
textures: &[],
|
||||
samplers: &[],
|
||||
vertices: &vertices,
|
||||
stride: 24,
|
||||
size: (32, 32),
|
||||
mesh_groups: 0,
|
||||
};
|
||||
let texels = host.draw(&root, &pso, &draw).unwrap();
|
||||
for (i, t) in texels.iter().enumerate() {
|
||||
let (x, y) = ((i % 32) as f32, (i / 32) as f32);
|
||||
let (u, v) = ((x + 0.5) / 16.0 - 1.0, 1.0 - (y + 0.5) / 16.0);
|
||||
let inside = u.abs() < 0.5 && v.abs() < 0.5;
|
||||
let want = if inside { [u, v, 1.0, 1.0] } else { [0.0; 4] };
|
||||
let near = t.iter().zip(want).all(|(a, b)| (a - b).abs() < 1e-4);
|
||||
assert!(near, "warp {warp} pixel ({x}, {y}): {t:?}, want {want:?}");
|
||||
for (n, order) in orders.iter().enumerate() {
|
||||
let vertices = order.concat();
|
||||
let draw = Draw {
|
||||
roots: Vec::new(),
|
||||
textures: &[],
|
||||
samplers: &[],
|
||||
vertices: &vertices,
|
||||
stride: 24,
|
||||
size: (32, 32),
|
||||
mesh_groups: 0,
|
||||
};
|
||||
let texels = host.draw(&root, &pso, &draw).unwrap();
|
||||
for (i, t) in texels.iter().enumerate() {
|
||||
let (x, y) = ((i % 32) as f32, (i / 32) as f32);
|
||||
let (u, v) = ((x + 0.5) / 16.0 - 1.0, 1.0 - (y + 0.5) / 16.0);
|
||||
let inside = n < 3 && u.abs() < 0.5 && v.abs() < 0.5;
|
||||
let want = if inside { [u, v, 1.0, 1.0] } else { [0.0; 4] };
|
||||
let near = t.iter().zip(want).all(|(a, b)| (a - b).abs() < 1e-4);
|
||||
assert!(near, "warp {warp} order {n} pixel ({x}, {y}): {t:?}, want {want:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user