diff --git a/crates/eval/src/graph_instance.rs b/crates/eval/src/graph_instance.rs index 99edbf9..b73d207 100644 --- a/crates/eval/src/graph_instance.rs +++ b/crates/eval/src/graph_instance.rs @@ -117,7 +117,8 @@ pub(crate) fn cook_instance( .ok_or_else(|| format!("the source output `{}` is missing", port.id))?; let output = result.outputs.first().ok_or_else(|| format!("the source output `{}` is empty", port.id))?; let source_base = result.base; - let port_base = crate::ResolvedBaseParameters { size: source_base.size, format: source_base.format, ..base }; + let port_base = + crate::ResolvedBaseParameters { size: source_base.size, size_underflow: source_base.size_underflow, format: source_base.format, ..base }; Ok((output.clone(), port_base)) }) .collect() diff --git a/crates/eval/src/node_execution.rs b/crates/eval/src/node_execution.rs index ca0c633..3686584 100644 --- a/crates/eval/src/node_execution.rs +++ b/crates/eval/src/node_execution.rs @@ -161,7 +161,7 @@ pub(crate) fn cook_node( let key = hash_of(&( &node.def, serde_json::to_string(¶meters).unwrap_or_default(), - (w, h, seed, tile, node.disabled, base.size, base.format, base.tiling, base.pixel_ratio, base.pixel_size.map(f32::to_bits)), + (w, h, seed, tile, node.disabled, base.size, base.size_underflow, base.format, base.tiling, base.pixel_ratio, base.pixel_size.map(f32::to_bits)), &input_keys, resource_hash, subgraph_hash, diff --git a/crates/eval/src/prepared_graph.rs b/crates/eval/src/prepared_graph.rs index e0ee0dd..6ff84fb 100644 --- a/crates/eval/src/prepared_graph.rs +++ b/crates/eval/src/prepared_graph.rs @@ -45,6 +45,7 @@ impl<'a> PreparedGraph<'a> { // Only bypassed generators take their image dimensions from the input if !node.disabled && definition.is_some_and(|definition| definition.generator) { base.size = self.parent_base.size; + base.size_underflow = self.parent_base.size_underflow; } Some(base) diff --git a/crates/eval/src/resolved_base_parameters.rs b/crates/eval/src/resolved_base_parameters.rs index f7a85b6..8fd57bb 100644 --- a/crates/eval/src/resolved_base_parameters.rs +++ b/crates/eval/src/resolved_base_parameters.rs @@ -5,6 +5,9 @@ use std::collections::BTreeMap; #[serde(rename_all = "camelCase")] pub struct ResolvedBaseParameters { pub size: [u32; 2], + /// Negative requested exponents on axes whose allocated texture is one texel. + #[serde(skip)] + pub size_underflow: [i32; 2], pub seed: u32, pub format: OutputFormat, pub tiling: [bool; 2], @@ -17,6 +20,7 @@ impl ResolvedBaseParameters { let parent = options.parent_base; Self { size: parent.map_or_else(|| graph.size_px(), |base| graph.output_size_px(base.size)), + size_underflow: parent.filter(|_| graph.absolute_output_size.is_none()).map_or([0; 2], |base| base.size_underflow), seed: parent.map_or(graph.seed as u32, |base| base.seed), format: parent.map_or(graph.format, |base| base.format), tiling: graph.tiling_mode.resolve(graph.tiling, options.parent_tiling, options.parent_tiling.unwrap_or(graph.tiling)), @@ -26,10 +30,14 @@ impl ResolvedBaseParameters { } pub(crate) fn resolve(base: &BaseParameters, input: Option, parent: Self) -> Self { - let size_base = BaseParameter::OutputSize.inheritance(base).resolve([1; 2], input.map(|base| base.size), parent.size); + let dimensions = |base: Self| (base.size, base.size_underflow); + let (size_base, underflow) = BaseParameter::OutputSize.inheritance(base).resolve(([1; 2], [0; 2]), input.map(dimensions), dimensions(parent)); + let shift: [i32; 2] = std::array::from_fn(|axis| underflow[axis].saturating_add(base.size[axis].clamp(-12, 13))); // Internal images may be one texel; the editor minimum would break size functions - let size = std::array::from_fn(|axis| (f64::from(size_base[axis]) * 2f64.powi(base.size[axis].clamp(-12, 13))).round().clamp(1.0, 8192.0) as u32); + let size = std::array::from_fn(|axis| (f64::from(size_base[axis]) * 2f64.powi(shift[axis])).round().clamp(1.0, 8192.0) as u32); + let size_underflow = + std::array::from_fn(|axis| if size_base[axis].is_power_of_two() { shift[axis].saturating_add(size_base[axis].ilog2() as i32).min(0) } else { 0 }); // Images carry size and format, but do not carry a random stream // Relative seed modes use the graph seed without accumulating upstream seeds @@ -37,6 +45,7 @@ impl ResolvedBaseParameters { let inherited_pixel_size = base.pixel_size_mode.resolve([1.0; 2], input.map(|base| base.pixel_size), parent.pixel_size); Self { size, + size_underflow, seed: inherited_seed.wrapping_add(base.seed as u32), format: BaseParameter::Format.inheritance(base).resolve(base.format, input.map(|base| base.format), parent.format), tiling: base.tiling_mode.resolve(base.tiling, input.map(|base| base.tiling), parent.tiling), @@ -46,9 +55,20 @@ impl ResolvedBaseParameters { } pub(crate) fn variables(self, variables: &mut BTreeMap) { + self.size_variables(variables); variables.insert("$tiling".into(), Value::Int(i32::from(self.tiling[0]) | (i32::from(self.tiling[1]) << 1))); variables.insert("$pixelratio".into(), Value::Int(self.pixel_ratio)); } + + fn size_variables(self, variables: &mut BTreeMap) { + if self.size_underflow == [0; 2] { + variables.remove("$sizelog2"); + } else { + let exponents = + std::array::from_fn(|axis| if self.size_underflow[axis] < 0 { self.size_underflow[axis] as f32 } else { (self.size[axis] as f32).log2() }); + variables.insert("$sizelog2".into(), Value::Float2(exponents)); + } + } } pub(crate) fn evaluate( @@ -92,7 +112,9 @@ pub(crate) fn evaluate( if let Some(function) = base.functions.get(¶meter) { resolved.variables(variables.to_mut()); let size = if parameter == BaseParameter::OutputSize && parameter.inheritance(base) != ParameterInheritance::Absolute { - parameter.inheritance(base).resolve(parent.size, input.map(|base| base.size), parent.size) + let inherited = parameter.inheritance(base).resolve(parent, input, parent); + inherited.size_variables(variables.to_mut()); + inherited.size } else { resolved.size }; diff --git a/crates/eval/src/tests.rs b/crates/eval/src/tests.rs index 8f469a8..c6d8bda 100644 --- a/crates/eval/src/tests.rs +++ b/crates/eval/src/tests.rs @@ -47,6 +47,40 @@ fn cooks_and_caches() { assert_eq!(outputs(&p.graphs[0], &c).len(), 1); } +#[test] +fn subtexel_size_exponents_survive_allocation_and_cache_reuse() { + use materialcraft_core::{FunctionGraph, OutputFormat, SizeMode}; + + let mut function = FunctionGraph::default(); + let size = function.add("get_float2", &[], Some(Value::Text("$size".into()))); + let log = function.add("get_float2", &[], Some(Value::Text("$sizelog2".into()))); + function.output = Some(function.add("vector4", &[size, log], None)); + let mut graph = Graph::new("Reduction"); + graph.size = [7; 2]; + graph.format = OutputFormat::F32; + let processor = graph.add_node("pixel_processor", [0.0; 2]); + let node = graph.node_mut(processor).unwrap(); + node.params.insert("function".into(), Value::Function(Box::new(function))); + node.params.insert("color_mode".into(), Value::Bool(true)); + node.base.size_mode = SizeMode::RelativeToParent; + let transform = graph.add_node("transformation", [120.0, 0.0]); + graph.node_mut(transform).unwrap().base.size = [3, 0]; + graph.connect(processor, "output", transform, "input").unwrap(); + let mut package = Package::new("Reduction"); + package.graphs.push(graph); + let cache = Mutex::new(Cache::default()); + + for (offset, first, log, next_width) in [(-8, -2147483648.0, -1.0, 4), (-9, 1073741824.0, -2.0, 2), (-10, 536870912.0, -3.0, 1)] { + package.graphs[0].node_mut(processor).unwrap().base.size = [offset, -2]; + let cooked = cook(&package, 0, &Options::new(&no_res), &cache); + assert!(cooked.nodes.values().all(|result| result.error.is_none() && !result.cached)); + let image = cooked.image(processor, 0).unwrap(); + assert_eq!((image.width(), image.height()), (1, 32)); + assert_eq!(image.get(0, 0, false), [first, 32.0, log, 5.0]); + assert_eq!(cooked.image(transform, 0).unwrap().width(), next_width); + } +} + #[test] fn processor_tiling_variable_tracks_both_axes_and_invalidates_cached_values() { let mut function = materialcraft_core::FunctionGraph::default(); diff --git a/crates/nodes/src/function/system_variable.rs b/crates/nodes/src/function/system_variable.rs index f3336ee..b614e33 100644 --- a/crates/nodes/src/function/system_variable.rs +++ b/crates/nodes/src/function/system_variable.rs @@ -82,8 +82,22 @@ impl SystemVariable { let value = match self { Self::Position => Value::Float2(context.position), Self::Depth | Self::DepthPow2 | Self::Number if context.scope == FunctionScope::ValueProcessor => Value::Float(0.0), - Self::Size => Value::Float2(context.size), - Self::SizeLog2 => Value::Float2(context.size.map(f32::log2)), + Self::Size | Self::SizeLog2 => { + let requested = match context.variables.get("$sizelog2") { + Some(Value::Float2(exponents)) => *exponents, + _ => [0.0; 2], + }; + Value::Float2(std::array::from_fn(|axis| { + if requested[axis] < 0.0 && context.size[axis] == 1.0 { + // The raster has one texel, but functions retain the signed size exponent. + if self == Self::SizeLog2 { requested[axis] } else { 1_i32.wrapping_shl(requested[axis] as i32 as u32) as f32 } + } else if self == Self::SizeLog2 { + context.size[axis].log2() + } else { + context.size[axis] + } + })) + } _ => context.variables.get(self.name()).cloned().unwrap_or_else(|| match self { Self::Tiling => Value::Int(if context.tile { 3 } else { 0 }), Self::DepthPow2 => Value::Float(1.0), diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 3fb0d7e..a1d85b0 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -253,6 +253,25 @@ Literal-only pixel graphs at 16², seed 23, give 512/768 exact pixels for each l The shared-lookup ridged root still matches 5,160/16,384 at changed roughness in F32, maximum 1.78813934e−7. Its one-octave .7 control still matches 1,847/4,096, maximum 2.38418579e−7. For positive normal x on the signed field, the reference's general power agrees with exp(y×ln(x)) on all 32,614 pixels. On .7 with exponent 1 it returns `3f333334`. Further work on general power and natural exponential is tracked with the [octave controls](#ridged-noise-octave-isolation). +### Alpha maximum and one-texel reductions + +The alpha-maximum helper matches **36/36 RGBA output pixels in 33 renders**, maximum error zero. Its output has one texel for inputs through 256² and four for a 512² input. The 128² position and alpha field previously selected a different tuple with maximum component error .968627451. Reading the whole reduction chain on this input showed that the final processor received an exact 2×2 image. At requested exponent −1 it samples the lower-right texel. The earlier cook treated the one-texel allocation as logical exponent zero. + +For the 128² field at seed 347, take u and v at texel centers and H=((59x+41y+5xy) mod 1021)/1020. Store (u,v,H,.125+.625H) as RGBA32F, replacing alpha at (3,125) by .9375. For fourteen further renders, use original RGBA8 fields with channels x mod 253, y mod 251, (31x+29y) mod 251 and (47x+59y+13xy) mod 191. Set a marked texel in quadrant j to (29+53j,211−47j,37+59j,255). Number quadrants in row-major order. Place the mark one eighth or seven eighths of the way across each axis. Use a 16² field at seed 73, four 128² fields at seeds 137, 173, 277 and 373, and a 64² southwest field in all four formats at seed 433. Also test 256² northeast/577, 512² southeast/661, 128×64 northwest/739 at a 128² parent, 128² with opposing northwest and southeast marks/809, and 128² southwest/953. These cases improved from 11/17 to 17/17 output pixels. The 128² northwest field returns RGBA8 levels (75,69,59,190) expressed as F32. The two-mark field returns (188,70,214,255). + +| Direct controls | Renders | Exact pixels or typed values / compared | Maximum error | +|---|---:|---:|---:| +| Pixel size, logarithm and sample displacement | 72 | 3,333/3,333 | 0 | +| Pixel canvas, absolute, parent and first input | 12 | 86,420/86,420 | 0 | +| Filter size relative to a one-texel input | 16 | 1,088/1,088 | 0 | +| Uniform color function | 8 | 148/148 | 0 | +| Stamp opacity | 24 | 444/444 | 0 | +| Typed value function | 24 | 24/24 | 0 | + +At parent sides 16, 32, 64, 128, 256 and 512, request offsets (−8,−8), (−5,−5), (−8,−2) and (−2,−8), seed 127. Return the two size components and the two pixel-center coordinates in RGBA32F. Repeat with the base-two size logarithm. For the third output, sample a linear F32 image at the pixel center plus (.25,.25) divided by the reported size. Set the source channels to (x/N,y/N,((17x+23y+9xy) mod 251)/250,1). The raster clamps each axis to one texel while the logarithm keeps the requested negative exponent. For exponents −1, −2, −3 and −4 the reported size is −2³¹, 2³⁰, 2²⁹ and 2²⁸. The position still uses physical texel centers. + +The propagation controls use parents of 16, 64, 128 and 256 at seed 131. Follow the (−8,−2) processor with a transform whose width offset relative to input is −1, 0, +1 or +3. For example, at parent 128 and offset +3 the transform is four texels wide. Uniform color encodes the two logarithms as .5 + log₂(size)/16 and the two reported sizes as .5 + size/2³². Test relative (−8,−2) and absolute (−2,3). A separate diagnostic that assigns a variable inside the pixel-ratio function, then reads it in another parameter, matches 64/296 pixels in sixteen renders. The reference returns zero for that read. This side effect remains open. + ### Material bundles and the eighth irradiance pass | Root | Recorded renders | Exact output pixels / compared | Maximum stored error | diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 2819a9c..8afcf33 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -35,7 +35,7 @@ A graph may accept input pins and still be a root under the dependency criterion Statuses: **Not checked**, **In progress**, **Parity** (exact pixels or typed values for the linked measured cases), or **Too hard** (reason recorded). Function calls do not count as regular graph dependencies. -The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **274 Parity**, **35 In progress** and **237 Too hard**. The November 17 and 18 comparisons include raw counts, output formats and remaining differences. [November 17 measurements](parity-fixes.md#2026-11-17-root-comparisons) · [November 18 measurements](parity-fixes.md#2026-11-18-root-comparisons) +The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **275 Parity**, **35 In progress** and **236 Too hard**. The November 17 and 18 comparisons include raw counts, output formats and remaining differences. [November 17 measurements](parity-fixes.md#2026-11-17-root-comparisons) · [November 18 measurements](parity-fixes.md#2026-11-18-root-comparisons) [Grayscale sampler measurements](parity-fixes.md#grayscale-sampling-of-color-inputs) · [Value sampling across 240 typed comparisons](parity-fixes.md#value-processor-texture-filtering) @@ -484,7 +484,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [quantize_color.sbs/color_conversions/convert_Lab_to_RGB](../assets/sbs/quantize_color.sbs) | 1 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | | [quantize_color.sbs/color_conversions/convert_RGB_to_Lab](../assets/sbs/quantize_color.sbs) | 1 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | | [quantize_color.sbs/create_color_palette_16colors](../assets/sbs/quantize_color.sbs) | 2 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | -| [quantize_color.sbs/get_min_max/GetMaxFromAlpha_v2](../assets/sbs/quantize_color.sbs) | 1 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | +| [quantize_color.sbs/get_min_max/GetMaxFromAlpha_v2](../assets/sbs/quantize_color.sbs) | 1 | Yes | [Parity · 33 renders, all 36 output pixels exact](parity-fixes.md#alpha-maximum-and-one-texel-reductions) | | [quantize_color.sbs/get_min_max/GetMaxFromAlpha_v3](../assets/sbs/quantize_color.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [quantize_color.sbs/helpers/cdf_pass](../assets/sbs/quantize_color.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [quantize_color.sbs/kmeans_iteration](../assets/sbs/quantize_color.sbs) | 2 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |