From 4734e90bf9b78a4dbe657645dd15504d22d7c079 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Mon, 23 Nov 2026 15:11:42 -0500 Subject: [PATCH] Display cooked graph colors in their stored color space --- crates/ui-egui/src/graph_pixels.rs | 26 +++++++++++++++++ crates/ui-egui/src/graph_previews.rs | 2 +- crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/preview_disk_cache.test.rs | 2 +- crates/ui-egui/src/thumbs.rs | 6 ++-- crates/ui-egui/src/view2d/display.rs | 28 ++++++------------- crates/ui-egui/src/view2d/pixel_readout.rs | 16 +++++------ crates/ui-egui/tests/snapshot/view2d.rs | 15 ++++++++++ docs/parity-fixes.md | 14 ++++++++++ docs/sbs-root-nodes.md | 2 ++ 10 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 crates/ui-egui/src/graph_pixels.rs diff --git a/crates/ui-egui/src/graph_pixels.rs b/crates/ui-egui/src/graph_pixels.rs new file mode 100644 index 00000000..392fd7a3 --- /dev/null +++ b/crates/ui-egui/src/graph_pixels.rs @@ -0,0 +1,26 @@ +//! Display levels of cooked graph images and node thumbnails. + +use materialcraft_engine::image::{Image, linear_to_srgb}; + +pub(crate) fn stored_byte(value: f32) -> u8 { + (value.clamp(0.0, 1.0) * 255.0 + 0.5) as u8 +} + +pub(crate) fn display_byte(value: f32, srgb: bool) -> u8 { + stored_byte(if srgb { value } else { linear_to_srgb(value) }) +} + +pub(crate) fn rgba8(image: &Image, srgb: bool) -> Vec { + let mut pixels = Vec::with_capacity(image.data().len() / image.channels() * 4); + if let Some(gray) = image.gray_data() { + for &value in gray { + let level = display_byte(value, srgb); + pixels.extend_from_slice(&[level, level, level, 255]); + } + } else { + for &[r, g, b, a] in image.data().as_chunks::<4>().0 { + pixels.extend_from_slice(&[display_byte(r, srgb), display_byte(g, srgb), display_byte(b, srgb), stored_byte(a)]); + } + } + pixels +} diff --git a/crates/ui-egui/src/graph_previews.rs b/crates/ui-egui/src/graph_previews.rs index 1be71bb7..1450cbaf 100644 --- a/crates/ui-egui/src/graph_previews.rs +++ b/crates/ui-egui/src/graph_previews.rs @@ -142,7 +142,7 @@ impl GraphPreviews { GraphPreviewKey::Native(id) => super::render_native(id), GraphPreviewKey::Graph(package, graph) => { NodeLibrary::preview_cancellable(&thread_library, package, graph, LIBRARY_PREVIEW_SIZE, &thread_cancel) - .map(|image| image.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display()) + .map(|image| crate::graph_pixels::rgba8(&image.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true), true)) } } }; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 4442b24d..12ddf28e 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -11,6 +11,7 @@ pub mod fonts; pub mod function; pub mod graph; mod graph_drag_payload; +mod graph_pixels; mod history; pub mod i18n; pub mod levels_editor; diff --git a/crates/ui-egui/src/preview_disk_cache.test.rs b/crates/ui-egui/src/preview_disk_cache.test.rs index 269906ee..835aba22 100644 --- a/crates/ui-egui/src/preview_disk_cache.test.rs +++ b/crates/ui-egui/src/preview_disk_cache.test.rs @@ -65,7 +65,7 @@ fn real_library_previews_survive_relaunch() { assert_eq!(phase, "cold", "warm launch must load {graph} without cooking"); materialcraft_engine::NodeLibrary::preview_cancellable(&library, package, graph, 72, &cancel) - .map(|image| image.resize(72, 72, true).to_rgba8_display()) + .map(|image| crate::graph_pixels::rgba8(&image.resize(72, 72, true), true)) }); assert_eq!(image.unwrap().len(), 72 * 72 * 4); diff --git a/crates/ui-egui/src/thumbs.rs b/crates/ui-egui/src/thumbs.rs index 19bc262b..c66cb291 100644 --- a/crates/ui-egui/src/thumbs.rs +++ b/crates/ui-egui/src/thumbs.rs @@ -24,10 +24,10 @@ pub struct Thumbs { frame: u64, } -/// An image as an egui texture image (sRGB display, `side` px) +/// A cooked graph image as an egui thumbnail, in its stored display space. pub fn color_image(img: &Image, side: u32) -> ColorImage { let small = img.resize(side, side, true); - ColorImage::from_rgba_unmultiplied([side as usize, side as usize], &small.to_rgba8_display()) + ColorImage::from_rgba_unmultiplied([side as usize, side as usize], &crate::graph_pixels::rgba8(&small, true)) } #[path = "graph_previews.rs"] @@ -170,5 +170,5 @@ fn render_native(id: &str) -> Option> { }; let images = (def.cook)(&ctx).ok()?; - Some(images.first()?.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true).to_rgba8_display()) + Some(crate::graph_pixels::rgba8(&images.first()?.resize(LIBRARY_PREVIEW_SIZE, LIBRARY_PREVIEW_SIZE, true), true)) } diff --git a/crates/ui-egui/src/view2d/display.rs b/crates/ui-egui/src/view2d/display.rs index e813a942..e988906b 100644 --- a/crates/ui-egui/src/view2d/display.rs +++ b/crates/ui-egui/src/view2d/display.rs @@ -1,21 +1,12 @@ //! The 8-bit pixels the 2D view draws for an image use materialcraft_engine::View2d; -use materialcraft_engine::image::{Image, Kind, linear_to_srgb}; +use materialcraft_engine::image::{Image, Kind}; /// 8-bit RGBA of `image` as the view's settings show it: the chosen channel, and brighter when /// the values are read as linear light pub(super) fn display_pixels(image: &Image, view: &View2d) -> Vec { - let mut pixels = image.to_rgba8_display(); - - if !view.srgb { - let encoded = encoded_levels(); - for pixel in pixels.as_chunks_mut::<4>().0 { - for value in pixel.iter_mut().take(3) { - *value = encoded[usize::from(*value)]; - } - } - } + let mut pixels = crate::graph_pixels::rgba8(image, view.srgb); let Some(channel) = isolated_channel(&view.channel).filter(|_| image.kind() == Kind::Color) else { return pixels }; @@ -37,15 +28,6 @@ pub(super) fn isolated_channel(channel: &str) -> Option { } } -/// Every 8-bit level read as linear light and encoded for the screen -fn encoded_levels() -> [u8; 256] { - let mut levels = [0; 256]; - for (level, encoded) in levels.iter_mut().enumerate() { - *encoded = (linear_to_srgb(level as f32 / 255.0).clamp(0.0, 1.0) * 255.0 + 0.5) as u8; - } - levels -} - #[cfg(test)] mod tests { use super::*; @@ -54,6 +36,12 @@ mod tests { Image::filled(2, 1, Kind::Color, [0.2, 0.4, 0.6, 0.5]) } + #[test] + fn encoded_graph_colors_keep_their_stored_levels_in_the_preview() { + let image = Image::filled(1, 1, Kind::Color, [0.2, 0.4, 0.8, 1.0]); + assert_eq!(display_pixels(&image, &View2d::default()), [51, 102, 204, 255]); + } + #[test] fn a_single_channel_is_shown_as_opaque_gray() { let all = display_pixels(&color(), &View2d::default()); diff --git a/crates/ui-egui/src/view2d/pixel_readout.rs b/crates/ui-egui/src/view2d/pixel_readout.rs index 6c2b191a..f85ec501 100644 --- a/crates/ui-egui/src/view2d/pixel_readout.rs +++ b/crates/ui-egui/src/view2d/pixel_readout.rs @@ -1,6 +1,8 @@ use egui::Color32; use materialcraft_engine::View2d; -use materialcraft_engine::image::{Image, linear_to_srgb}; +use materialcraft_engine::image::Image; + +use crate::graph_pixels::{display_byte, stored_byte}; /// The hue is listed in degrees around the color wheel const HUE_DEGREES: f32 = 360.0; @@ -39,14 +41,10 @@ impl PixelReadout { /// The opaque sample with the view's display encoding and channel choice pub(super) fn swatch(&self, view: &View2d) -> Color32 { - let level = |value: f32| (value.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; - let shown = |value: f32| if self.gray { level(value) } else { level(linear_to_srgb(value)) }; - let mut rgb = [shown(self.color[0]), shown(self.color[1]), shown(self.color[2])]; - if !view.srgb { - rgb = rgb.map(|value| level(linear_to_srgb(f32::from(value) / 255.0))); - } + let [r, g, b, a] = self.color; + let mut rgb = [r, g, b].map(|value| display_byte(value, view.srgb)); if let Some(channel) = super::display::isolated_channel(&view.channel).filter(|_| !self.gray) { - let value = if channel == 3 { level(self.color[3]) } else { rgb[channel] }; + let value = if channel == 3 { stored_byte(a) } else { rgb[channel] }; rgb = [value; 3]; } Color32::from_rgb(rgb[0], rgb[1], rgb[2]) @@ -135,7 +133,7 @@ mod tests { assert_eq!(PixelReadout::at(&gray, [0, 0]).swatch(&View2d::default()), Color32::from_rgb(128, 128, 128)); let color = Image::filled(1, 1, Kind::Color, [0.5, 0.0, 1.0, 1.0]); - assert_eq!(PixelReadout::at(&color, [0, 0]).swatch(&View2d::default()), Color32::from_rgb(188, 0, 255)); + assert_eq!(PixelReadout::at(&color, [0, 0]).swatch(&View2d::default()), Color32::from_rgb(128, 0, 255)); } #[test] diff --git a/crates/ui-egui/tests/snapshot/view2d.rs b/crates/ui-egui/tests/snapshot/view2d.rs index f72e2e8d..7a8238a3 100644 --- a/crates/ui-egui/tests/snapshot/view2d.rs +++ b/crates/ui-egui/tests/snapshot/view2d.rs @@ -23,6 +23,21 @@ fn save(harness: &mut Harness<'static, MaterialCraftApp>, name: &str) { harness.render().unwrap().save(directory.join(format!("{name}.png"))).unwrap(); } +#[test] +#[ignore = "requires an offscreen GPU; run explicitly with --ignored"] +fn the_2d_view_displays_stored_graph_colors() { + let mut app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default()); + app.session.preview_size = Some(64); + let color = app.run("graph.node.add", json!({"def": "uniform", "params": {"color": [0.2, 0.4, 0.8, 1.0]}})).unwrap()["id"].as_u64().unwrap(); + app.run("view.2d", json!({"node": color})).unwrap(); + app.session.cook(); + let mut harness = view(app); + harness.run_steps(4); + let image = harness.render().unwrap(); + assert_eq!(image.get_pixel(image.width() / 2, image.height() / 2).0, [51, 102, 204, 255]); + save(&mut harness, "view2d-encoded-color"); +} + #[test] #[ignore = "requires an offscreen GPU; run explicitly with --ignored"] fn the_2d_view_shows_its_settings() { diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 95ab80a8..29637a91 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -59,6 +59,20 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas ## 2026-11-23 root comparisons +### Stored graph color in the 2D view + +Display the Base Color output of Bricks 01 at 1:1 with the default sRGB interpretation. With seed 0 and a 64² parent, all 4,096 pixels captured from the reference viewport match its raw U8 RGB output, maximum error 0. The old MaterialCraft viewport applied a linear-light to sRGB conversion to those already encoded numbers. The same conversion brightened graph thumbnails and the color sampler. + +Use a uniform color with stored RGB (.2,.4,.8) and alpha 1. The reference viewport and node thumbnail show (51,102,204). The old MaterialCraft preview showed (124,170,231). The corrected MaterialCraft offscreen viewport shows (51,102,204). For the full byte scale, make 86 original opaque swatches whose three channels cover levels 0–255. Compare the rendered reference colors and the actual MaterialCraft display function. With the default encoded interpretation, 86/86 swatches and 258/258 channels match, maximum error 0. Reading the same numbers as linear light gives 14/86 exact swatches and 132/258 exact channels, maximum error 1. In seven further storage controls across U8, U16, F16 and F32, both interpretations stay within one displayed byte. The display choice leaves stored channel readouts and alpha intact. + +| Bricks 01 Base Color | Reference mean RGB | Old mean RGB | Corrected mean RGB | Old exact pixels / max error | Corrected exact pixels / max error | +|---|---|---|---|---:|---:| +| 64², seed 0, defaults | 152.2, 119.9, 97.6 | 203.6, 182.1, 165.3 | 154.8, 121.5, 98.4 | 0/4,096 · 154 | 628/4,096 · 119 | +| 128², seed 0, defaults | 153.3, 117.0, 93.6 | 204.7, 180.3, 162.0 | 156.7, 119.0, 94.3 | 0/16,384 · 155 | 3,112/16,384 · 125 | +| 64², seed 19, luminosity .65 | 218.8, 201.3, 190.8 | 237.7, 228.4, 222.7 | 217.8, 199.8, 189.2 | 97/4,096 · 121 | 900/4,096 · 118 | + +The comparison uses the same cooked MaterialCraft buffer for each old and corrected pair. A pixel counts when all three displayed bytes match. Maximum error is the largest absolute channel difference in byte levels. On the 64² default, the specular texture already matched at 4,096/4,096 raw pixels and now also matches at 4,096/4,096 displayed pixels, maximum error 0. The remaining base-color differences are present in the raw graph output. [First differing tile and noise stages](#tile-luminance-variation-in-bricks-01) + ### Tile luminance variation in Bricks 01 Cook the base-color output of the Bricks 01 PBR material at 64², default inputs and seed 0, with one reference worker. Read the raw ports of its 144 top-level nodes. The base-color branch has 123 nodes. The first differing outputs have matching upstream images, including the 16² U8 Source image connected to both tile instances (256/256, maximum error 0). diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index dd88cbe0..0ef483a2 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -41,6 +41,8 @@ Nested Curvature, Curvature Smooth and Normal Sobel each match 2,304/2,304 origi Ordinary SBS saves preserve graph seed inheritance across 22 nested graphs. The original and reopened random fields each match 22,528/22,528 pixels. BNW Spots 1 and Clouds 1 and 2 each match 9,216/9,216 pixels across four output formats in the two nested settings. [Seed settings, whole graphs and renderer repeats](parity-fixes.md#graph-random-seed-inheritance-on-save-and-in-nested-graphs) +The Bricks 01 material provides a separate 2D display check. The corrected preview matches all 258 tested encoded RGB channels. At 64², its base-color mean is (155,122,98) against (152,120,98) in the reference. The cooked material still differs. [Color swatches and material measurements](parity-fixes.md#stored-graph-color-in-the-2d-view) + Tile Generator has reopened. The 4 × 14 grayscale grid matches at seeds 0 and 17 until luminance variation is enabled. The isolated setting matches 0/4,096 at each seed. [Bricks 01 tile and noise stages](parity-fixes.md#tile-luminance-variation-in-bricks-01) BNW Spots 1, Clouds 1 and Clouds 2 each match 9,216/9,216 direct U16 pixels and a further 9,216/9,216 across four actual output formats. Reopened graphs have the same counts and maximum error 0. The 256² differences remain open. [Nested sizes, seeds and output formats](parity-fixes.md#computed-caller-sizes-on-bnw-spots-1-and-clouds-1-and-2)