Retain product factors through copied pixel variables

This commit is contained in:
chasse20 committed 2026-11-23 03:25:45 -05:00
1 parent a63532827f
commit 2abc3383bd
4 files changed
+77 -9

No files matched your search

+38 -7
View File
@@ -42,6 +42,10 @@ fn variable(op: &Instruction) -> Option<&str> {
}
}
fn float_getter(op: &Instruction) -> bool {
matches!(op.definition.id, "get_float1" | "get_float2" | "get_float3" | "get_float4")
}
fn product(op: &Instruction) -> Option<(usize, usize)> {
let &[a, b] = op.inputs.as_slice() else { return None };
(!op.literal && float_width(op.type_code) > 0 && matches!(op.definition.id, "mul" | "mulscalar")).then_some((a, b))
@@ -67,7 +71,7 @@ struct Uses {
}
impl Uses {
fn analyze(program: &Program) -> Self {
fn analyze(program: &Program, caller_reads: &dyn Fn(&str) -> u8) -> Self {
let len = program.instructions.len();
let mut uses = Self { rounded: vec![0; len], live: vec![false; len], ordered: true, ..Self::default() };
if let (Some(mask), Some(live)) = (uses.rounded.get_mut(program.output), uses.live.get_mut(program.output)) {
@@ -86,7 +90,9 @@ impl Uses {
}
};
if let Some(call) = &op.call {
let child = Self::analyze(&call.program);
let child = Self::analyze(&call.program, &|name| {
if call.defaults.contains_key(name) { 0 } else { caller_reads(name) | uses.reads.get(name).copied().unwrap_or(0) }
});
for (name, register) in call.inputs.iter().zip(&op.inputs) {
mark(*register, child.reads.get(name).copied().unwrap_or(0));
}
@@ -133,7 +139,18 @@ impl Uses {
}
}
("set", &[input]) => {
mark(input, if program.instructions.get(input).and_then(product).is_some() { observed } else { 15 });
let source = program.instructions.get(input);
// Assigning a getter copies its value. Its rounded lanes are the ones read from either name.
let mask = if source.is_some_and(float_getter)
&& let Some(name) = variable(op)
{
observed | caller_reads(name) | uses.reads.get(name).copied().unwrap_or(0)
} else if source.and_then(product).is_some() {
observed
} else {
15
};
mark(input, mask);
if let Some(name) = variable(op) {
*uses.writes.entry(name.to_owned()).or_default() += 1;
}
@@ -168,12 +185,12 @@ fn has_assignment(program: &Program) -> bool {
pub(super) fn identify(program: &mut Program) {
if program.instructions.iter().any(|op| op.call.is_some()) && has_assignment(program) {
let uses = Uses::analyze(program);
let uses = Uses::analyze(program, &|_| 0);
install(program, &uses, &uses.reads, BTreeMap::new());
}
}
fn install(program: &mut Program, uses: &Uses, observed: &ReadMasks, mut names: BTreeMap<String, Binding>) {
fn install(program: &mut Program, uses: &Uses, observed: &ReadMasks, mut names: BTreeMap<String, Binding>) -> BTreeMap<String, Binding> {
let instructions = &program.instructions;
// A dynamic branch can skip a write. Only a unique, executed assignment is eligible in that case.
if !uses.ordered {
@@ -232,9 +249,21 @@ fn install(program: &mut Program, uses: &Uses, observed: &ReadMasks, mut names:
imported.insert(name.clone(), binding.import(&mut call.assigned_products));
}
}
install(&mut call.program, child, observed, imported);
let returned = install(&mut call.program, child, observed, imported);
if uses.ordered {
names.retain(|name, _| !child.writes.contains_key(name) || call.defaults.contains_key(name));
// A free name assigned a caller's step can carry the same factors into a later call.
if child.ordered {
for (name, binding) in returned {
if child.writes.contains_key(&name)
&& !call.defaults.contains_key(&name)
&& let Registers::Argument(slot) = binding.registers
&& let Some(&argument) = call.assigned_products.get(slot)
{
names.insert(name, argument);
}
}
}
}
continue;
}
@@ -242,7 +271,8 @@ fn install(program: &mut Program, uses: &Uses, observed: &ReadMasks, mut names:
&& op.definition.id == "set"
&& let Some(name) = variable(op)
{
if let Some(&binding) = setters.get(&index) {
let copied = op.inputs.first().and_then(|&i| previous.get(i).filter(|op| float_getter(op)).and_then(|_| source(i)));
if let Some(binding) = setters.get(&index).copied().or(copied) {
names.insert(name.to_owned(), binding);
} else {
names.remove(name);
@@ -271,4 +301,5 @@ fn install(program: &mut Program, uses: &Uses, observed: &ReadMasks, mut names:
}
}
}
names
}
+23 -2
View File
@@ -570,6 +570,8 @@ enum StepRead {
Blue,
BeforeWrite,
AfterWrite,
Copy,
CopyBlue,
}
fn called_pixel_step(sample: [f32; 4], wired: bool, guard: Option<bool>, read: StepRead) -> Value {
@@ -607,6 +609,14 @@ fn called_pixel_step(sample: [f32; 4], wired: bool, guard: Option<bool>, read: S
let sum = body.add("add", &[origin, step], None);
body.output = Some(match read {
StepRead::Sum | StepRead::Blue => sum,
StepRead::Copy | StepRead::CopyBlue => {
let copy = body.add("set", &[step], Some(Value::Text("saved_step".into())));
let replace = body.add("set", &[origin], Some(Value::Text("stride".into())));
let effects = body.add("sequence", &[copy, replace], None);
let saved = body.add("get_float4", &[], Some(Value::Text("saved_step".into())));
let advance = body.add("add", &[origin, saved], None);
body.add("sequence", &[effects, advance], None)
}
StepRead::BeforeWrite => {
let answer = body.add("set", &[sum], Some(Value::Text("answer".into())));
let write = body.add("set", &[origin], Some(Value::Text("stride".into())));
@@ -631,9 +641,10 @@ fn called_pixel_step(sample: [f32; 4], wired: bool, guard: Option<bool>, read: S
});
node.inputs = args.iter().map(|(_, id)| Some(*id)).collect();
node.source = Some(Ok(std::sync::Arc::new(body)));
let result = if matches!(read, StepRead::Blue) {
let result = if matches!(read, StepRead::Blue | StepRead::CopyBlue) {
let color = graph.add("swizzle3", &[call], Some(Value::Int3([0, 1, 2])));
let stored = graph.add("get_float4", &[], Some(Value::Text("stride".into())));
let name = if matches!(read, StepRead::CopyBlue) { "saved_step" } else { "stride" };
let stored = graph.add("get_float4", &[], Some(Value::Text(name.into())));
let blue = graph.add("swizzle1", &[stored], Some(Value::Int(2)));
graph.add("vector4", &[color, blue], None)
} else {
@@ -676,6 +687,16 @@ fn called_pixel_sums_follow_assignments_in_execution_order() {
assert_eq!(called_pixel_step(sample, false, None, StepRead::AfterWrite), Value::Float4(origin.map(|v| v + v)));
}
#[test]
fn called_pixel_sums_keep_factors_through_a_copied_step() {
let sample = [0xbed0_b7e6, 0x3ee9_4b78, 0xbf01_df0b, 0xbf9a_729d].map(f32::from_bits);
for (read, expected) in
[(StepRead::Copy, [0x3f88_ad30, 0xbc7c_be30, 0xbfe0_d289, 0x3f86_2e37]), (StepRead::CopyBlue, [0x3f88_ad30, 0xbc7c_be30, 0xbfe0_d28a, 0xbf0c_bfd9])]
{
assert_eq!(called_pixel_step(sample, false, None, read), Value::Float4(expected.map(f32::from_bits)));
}
}
#[test]
fn pixel_product_sums_round_once_unless_the_product_is_also_read() {
let values = [0x3fe34b19, 0x400d3c6f, 0xc0732711, 0x3f9665e4].map(|bits| Value::Float(f32::from_bits(bits)));
+14
View File
@@ -59,6 +59,20 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas
## 2026-11-23 root comparisons
### Copied steps in called pixel sums
The shared pixel evaluator now follows a floating-point product copied to another variable. A later addition can use its original factors after the first variable is overwritten. Reading a component of the copy separately rounds that component. The same behavior applies when the next addition or the component read is in the caller or a subsequent function call.
Use the four F32 settings from the [component and assignment-order comparison](#component-reads-and-writes-across-pixel-function-calls). From the original signed input field form a position from GBAR and a direction from BRGA. Store direction times A as the step. Inside the call, copy the step to another variable, overwrite the original with the position, and add the captured step to the position. Compare a single unused copy, a second unused copy, an explicit read of the copy, and a read in a later call. In three further graphs, append the saved B component after the first three sum components, reading B inside the call or in the caller. As a control, overwrite the copy with the position before reading it.
| Comparison | Graphs | Settings | Exact F32 pixels / total | Maximum component error |
|---|---:|---:|---:|---:|
| Original graphs, before | 8 | 32 | 23,916/49,152 | 4.76837158e−7 |
| Same graphs, corrected | 8 | 32 | 49,152/49,152 | 0 |
| Copy, separate B read, caller B read and later call saved by MaterialCraft | 4 | 16 | 24,576/24,576 | 0 |
All four saves reproduced the original reference pixels when reopened. The earlier unused-copy graph improved from 3,968/8,192 to 8,192/8,192 in F16 and F32 combined. Of the 42 earlier assignment graphs, 41 now match 342,016/342,016 pixels across 197 settings. The sampled-alpha branch still matches 11,173/22,528, including when reopened. Another 49 earlier arithmetic graphs retained 1,288,192/1,288,192 exact pixels across 307 settings. The 64² irradiance first processor retained 4,096/4,096 in both floating formats. The copied-step regression failed before the change. All 401 node release tests, formatting, strict workspace Clippy, dependency layers and WebAssembly checks passed.
### Switch Blend with parameter-driven endpoint opacity
An unmasked Switch Blend with opacity 0 or 1 can forward the selected floating-point color texture when opacity is fixed and storage precision matches. When its opacity reads an exposed graph parameter, the color conversion runs even at matching precision. It flushes F32 subnormal components to signed zero, then clears zero signs unless all three RGB signs are negative. A change of floating-point storage precision also runs this conversion. The shared Blend cook now uses the evaluator's existing distinction between fixed and parameter-driven opacity.
+2
View File
@@ -43,6 +43,8 @@ The eighth specular pass in the second package (root 408) matches all 143,300 or
In the first package, all 17 default-index irradiance settings match 120,320/120,320 pixels, with 4,537/4,608 exact across nine connected-index settings. All 26 next-index values match. For the first eighth specular pass, parameter-driven Switch Blend now reproduces the three differing green zero signs at 256² in a coordinate probe. [Irradiance bridge and typed-output measurements](parity-fixes.md#eighth-irradiance-pass-with-default-and-connected-indices) · [Switch Blend controls and saved files](parity-fixes.md#switch-blend-with-parameter-driven-endpoint-opacity)
Eight original called-function graphs now match all 49,152 F32 pixels when a step is copied before a later assignment. Four saves reopened in both applications and matched another 24,576 pixels. The sampled-alpha assignment case remains open. [Copied steps and retained arithmetic comparisons](parity-fixes.md#copied-steps-in-called-pixel-sums)
In the two eighth specular passes, the corrected pixel arithmetic matched all 2,304 scanline pixels across zero through eight contributions. The changed 128² pass 396 scores 16,384/16,384 · 0 in F16 and the default 128² pass 408 scores 16,384/16,384 · 0. All 376,832 pixels in the direct and reusable function graphs match, as do all 131,072 reopened pixels. [Raw counts and procedural inputs](parity-fixes.md#reciprocal-division-through-pixel-function-calls)
Twelve original ancestor-offset controls matched 61,440/61,440 pixels. Three reopens matched another 15,360/15,360. In the two dense BNW Spots 3 packages, the tested square F32 settings that completed matched 4,608/4,608 pixels on the explicit one-worker renderer. Three settings still reached the execution budget. [Returning squares, renderer repeats and per-format counts](parity-fixes.md#branch-offsets-center-culling-and-two-dense-spot-graphs)