diff --git a/crates/compose/src/below.rs b/crates/compose/src/below.rs index 61bf2e95..81868e66 100644 --- a/crates/compose/src/below.rs +++ b/crates/compose/src/below.rs @@ -178,6 +178,52 @@ impl StackKey { } } +impl StackKey { + /// The tiles whose pixels differ between `self` and `other` when nothing but painted pixels + /// changed (same set, roots, layers, baked maps and images; the same images by id, channel + /// and size). None when anything else differs: the caller recomposites everything. + pub fn changed_tiles(&self, other: &StackKey) -> Option> { + if self.set != other.set || self.roots != other.roots || self.layers != other.layers || self.baked != other.baked || self.images != other.images { + return None; + } + if self.pixels.len() != other.pixels.len() { + return None; + } + let size = self.set.size(); + let mut changed = BTreeSet::new(); + for ((left_id, left_channel, left_image), (right_id, right_channel, right_image)) in self.pixels.iter().zip(&other.pixels) { + let (left_image, right_image) = (&left_image.0, &right_image.0); + let same_shape = left_id == right_id + && left_channel == right_channel + && left_image.width() == right_image.width() + && left_image.height() == right_image.height() + && left_image.comps() == right_image.comps(); + + // Image tiles are composite tiles only when the grids coincide + if !same_shape || (left_image.width(), left_image.height()) != size { + return None; + } + if left_image.same_tiles(right_image) { + continue; + } + + for tile_y in 0..left_image.tiles_y() { + for tile_x in 0..left_image.tiles_x() { + let equal = match (left_image.tile_handle(tile_x, tile_y), right_image.tile_handle(tile_x, tile_y)) { + (None, None) => true, + (Some(left_tile), Some(right_tile)) => Arc::ptr_eq(left_tile, right_tile), + _ => false, + }; + if !equal { + changed.insert((tile_x, tile_y)); + } + } + } + } + Some(changed) + } +} + /// An image compared by tile identity: holding the clone keeps its tiles alive, and a tile /// rewritten since is a different allocation (copy-on-write), so pointer equality means the /// same pixels without reading them. @@ -186,17 +232,18 @@ struct Shared(TileImage); impl PartialEq for Shared { fn eq(&self, other: &Self) -> bool { - let (a, b) = (&self.0, &other.0); - a.width() == b.width() - && a.height() == b.height() - && a.comps() == b.comps() - && (0..a.tiles_y()).all(|ty| { - (0..a.tiles_x()).all(|tx| match (a.tile_handle(tx, ty), b.tile_handle(tx, ty)) { - (None, None) => true, - (Some(x), Some(y)) => Arc::ptr_eq(x, y), - _ => false, - }) - }) + let (left_image, right_image) = (&self.0, &other.0); + left_image.width() == right_image.width() + && left_image.height() == right_image.height() + && left_image.comps() == right_image.comps() + && (left_image.same_tiles(right_image) + || (0..left_image.tiles_y()).all(|tile_y| { + (0..left_image.tiles_x()).all(|tile_x| match (left_image.tile_handle(tile_x, tile_y), right_image.tile_handle(tile_x, tile_y)) { + (None, None) => true, + (Some(left_tile), Some(right_tile)) => Arc::ptr_eq(left_tile, right_tile), + _ => false, + }) + })) } } @@ -416,4 +463,48 @@ mod tests { c.set_hot(Some(LayerId(3))); assert!(c.is_empty() && c.bytes() == 0); } + + /// A project with one paint layer in a 128 px set, its pixels and a key builder. + fn painted() -> (Project, PixelStore, LayerId) { + let mut project = Project::new("t", "builtin:plane"); + project.texture_sets.push(TextureSet::new("Plane", 0, 128)); + let id = project.alloc_id(); + let layer = Layer::new(id, "Paint", LayerKind::Paint, &[Channel::BaseColor]); + project.insert_layer(0, layer, None, false).unwrap(); + (project, PixelStore::default(), id) + } + + fn key(project: &Project, pixels: &PixelStore) -> StackKey { + let stack = project.texture_sets[0].stack.clone(); + StackKey::new(project, pixels, &BakedMaps::default(), &ImageStore::default(), 0, &stack).unwrap() + } + + #[test] + fn changed_tiles_lists_only_rewritten_tiles() { + let (project, mut pixels, id) = painted(); + pixels.layer_mut(id, Channel::BaseColor, (128, 128)).set(70, 5, &[9, 9, 9, 9]); + let before = key(&project, &pixels); + pixels.layer_mut(id, Channel::BaseColor, (128, 128)).set(70, 6, &[1, 1, 1, 1]); + let after = key(&project, &pixels); + assert_eq!(before.changed_tiles(&after), Some(BTreeSet::from([(1, 0)]))); + } + + #[test] + fn changed_tiles_is_none_when_a_layer_changes() { + let (mut project, pixels, id) = painted(); + let before = key(&project, &pixels); + project + .layer_mut(id) + .unwrap() + .channels + .insert(Channel::BaseColor, texturecraft_core::ChannelBlend { mode: texturecraft_core::BlendMode::Multiply, opacity: 0.5 }); + assert_eq!(before.changed_tiles(&key(&project, &pixels)), None); + } + + #[test] + fn changed_tiles_is_empty_for_identical_documents() { + let (project, mut pixels, id) = painted(); + pixels.layer_mut(id, Channel::BaseColor, (128, 128)).set(3, 3, &[5, 5, 5, 5]); + assert_eq!(key(&project, &pixels).changed_tiles(&key(&project, &pixels)), Some(BTreeSet::new())); + } } diff --git a/crates/compose/src/display.rs b/crates/compose/src/display.rs index 190a6c5a..9fce5480 100644 --- a/crates/compose/src/display.rs +++ b/crates/compose/src/display.rs @@ -6,13 +6,16 @@ //! |-----------|----------|-------------------|----------------|-------------------|-------------------| //! | BaseColor | sRGB8 | base color | | | opacity | //! | Orm | RGBA8 | AO (× baked) | roughness | metallic | specular level | -//! | Normal | RGBA8 | normal x | normal y | normal z | sheen roughness | +//! | Normal | RGBA16F | normal x | normal y | normal z | sheen roughness | //! | Height | RGBA16F | height | Specular (F0) r| g | b | //! | Emissive | sRGB8 | emissive | | | scattering weight | //! | Lobes | RGBA8 | coat weight | coat roughness | anisotropy level | anisotropy angle | //! | Sheen | sRGB8 | sheen color | | | sheen weight | //! | Extra | RGBA8 | coat normal x | coat normal y | transmission | tile scale | //! +//! Normal holds the tangent-space normal signed (flat is `(0, 0, 1)`) in half floats: baked +//! normals are high-precision data, and 8 bits per axis bands smooth surfaces in the shading. +//! //! Extra packs the coat normal's x and y as `128 + 127 × n` (the default normal is exactly 0; //! z is rebuilt in the shader). Its alpha is the [`Composite::block_scales`] of the texel's UV //! tile as `32 × log2(1 / scale)` (0 for a tile filling its block): the shader turns height @@ -36,7 +39,7 @@ pub enum DisplayKind { /// RGBA8 linear: ambient occlusion (channel × baked), roughness (1 − glossiness when the set /// has a Glossiness channel), metallic, specular level. Orm, - /// RGBA8: painted tangent-space normal. + /// RGBA16F: the shown tangent-space normal, signed (flat is `(0, 0, 1)`), alpha = sheen roughness. Normal, /// RGBA16F: height in r, the linear Specular (F0) color in gba. Height, @@ -64,7 +67,7 @@ impl DisplayKind { /// Bytes per texel of the upload format. pub fn bytes_per_texel(self) -> usize { - if self == DisplayKind::Height { 8 } else { 4 } + if matches!(self, DisplayKind::Height | DisplayKind::Normal) { 8 } else { 4 } } /// Display textures affected by a channel. @@ -131,6 +134,7 @@ pub fn tile_bytes(composite: &Composite, baked: &BakedMaps, kind: DisplayKind, t }; let (src, alpha) = (get(c), scalar8(alpha)); let d = c.default_value().map(|v| (texturecraft_texel::linear_to_srgb(v) * 255.0 + 0.5) as u8); + for i in 0..TILE_AREA { let pixel = &mut out[i * 4..i * 4 + 4]; match src { @@ -146,6 +150,7 @@ pub fn tile_bytes(composite: &Composite, baked: &BakedMaps, kind: DisplayKind, t } DisplayKind::Lobes => { let lanes = [Channel::CoatWeight, Channel::CoatRoughness, Channel::AnisotropyLevel, Channel::AnisotropyAngle].map(scalar8); + for (i, pixel) in out.as_chunks_mut::<4>().0.iter_mut().enumerate() { for (d, lane) in pixel.iter_mut().zip(&lanes) { *d = lane(i); @@ -175,12 +180,14 @@ pub fn tile_bytes(composite: &Composite, baked: &BakedMaps, kind: DisplayKind, t let gloss = composite.channels.contains_key(&Channel::Glossiness); let (r, r_default) = if gloss { (get(Channel::Glossiness), default16(Channel::Glossiness)) } else { (get(Channel::Roughness), default16(Channel::Roughness)) }; + for i in 0..TILE_AREA { let (x, y) = (tx * TILE + (i as u32 % TILE), ty * TILE + (i as u32 / TILE)); let baked_ao = baked .sample_working(BakedMap::AmbientOcclusion, x, y, (resolution, composite.height), &composite.block_scales) .map_or(1.0, |value| value[0]); let ch_ao = ao.and_then(|a| a.get(i)).map(|v| *v as f32 / 65535.0).unwrap_or(1.0); + let pixel = &mut out[i * 4..i * 4 + 4]; pixel[0] = ((baked_ao * ch_ao).clamp(0.0, 1.0) * 255.0 + 0.5) as u8; let v = r.and_then(|t| t.get(i).copied()).unwrap_or(r_default); @@ -190,34 +197,36 @@ pub fn tile_bytes(composite: &Composite, baked: &BakedMaps, kind: DisplayKind, t } } DisplayKind::Normal => { - let (n, sheen_roughness) = (get(Channel::Normal), scalar8(Channel::SheenRoughness)); - for i in 0..TILE_AREA { - let pixel = &mut out[i * 4..i * 4 + 4]; + let (normal_tile, sheen_roughness_tile) = (get(Channel::Normal), get(Channel::SheenRoughness)); + let sheen_default = Channel::SheenRoughness.default_value()[0]; + let has_baked_normal = baked.maps.contains_key(&BakedMap::Normal); - // The baked normal is the base the painted normal details sit on (as in export) + for (i, pixel) in out.as_chunks_mut::<8>().0.iter_mut().enumerate() { + // Baked normals form the base for painted detail, matching export let (x, y) = (tx * TILE + (i as u32 % TILE), ty * TILE + (i as u32 / TILE)); - let base = baked.sample_working(BakedMap::Normal, x, y, (resolution, composite.height), &composite.block_scales); - match (n, base) { - (Some(t), None) => { - for (k, d) in pixel.iter_mut().take(3).enumerate() { - *d = u8_of(t.get(i * 3 + k).copied().unwrap_or(0)); - } - } - (detail, Some(base)) => { - let detail = detail.map_or([0.5, 0.5, 1.0], |t| [0, 1, 2].map(|k| t.get(i * 3 + k).map_or(0.0, |v| *v as f32 / 65535.0))); - let v = texturecraft_core::blend::rnm(base, detail); - for (d, v) in pixel.iter_mut().zip(v) { - *d = (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; - } - } - (None, None) => pixel[..3].copy_from_slice(&[128, 128, 255]), + let baked_sample = + if has_baked_normal { baked.sample_working(BakedMap::Normal, x, y, (resolution, composite.height), &composite.block_scales) } else { None }; + + // Invalid baked samples must not enter half-float shading + let base = baked_sample.filter(|normal| normal.iter().all(|component| (0.0..=1.0).contains(component))); + let painted = normal_tile.map(|tile| [0, 1, 2].map(|k| tile.get(i * 3 + k).map_or(0.0, |v| *v as f32 / 65535.0))); + let packed = match (painted, base) { + (Some(painted), None) => painted, + (painted, Some(base)) => texturecraft_core::blend::rnm(base, painted.unwrap_or([0.5, 0.5, 1.0])), + (None, None) => [0.5, 0.5, 1.0], + }; + + let sheen = sheen_roughness_tile.and_then(|tile| tile.get(i)).map_or(sheen_default, |v| *v as f32 / 65535.0); + let [x, y, z] = packed.map(|v| (v * 2.0 - 1.0).clamp(-1.0, 1.0)); + for (k, c) in [x, y, z, sheen].into_iter().enumerate() { + pixel[k * 2..k * 2 + 2].copy_from_slice(&half::f16::from_f32(c).to_le_bytes()); } - pixel[3] = sheen_roughness(i); } } DisplayKind::Height => { let (h, spec) = (get(Channel::Height), get(Channel::Specular)); let spec_default = Channel::Specular.default_value(); + for i in 0..TILE_AREA { let v = h.and_then(|t| t.get(i)).map(|v| texturecraft_texel::from_u16_signed(*v)).unwrap_or(0.0); let f0 = |k: usize| spec.and_then(|t| t.get(i * 3 + k)).map(|v| *v as f32 / 65535.0).unwrap_or(spec_default[k]); @@ -264,7 +273,9 @@ mod tests { let mut comp = Composite::new(&[Channel::BaseColor, Channel::Opacity], 64); comp.channels.get_mut(&Channel::BaseColor).unwrap().set(0, 0, &[to_u16(1.0), to_u16(0.5), 0]); comp.channels.get_mut(&Channel::Opacity).unwrap().set(0, 0, &[to_u16(0.5)]); + let b = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::BaseColor, 0, 0); + assert_eq!(b[0], 255); assert!((b[1] as i32 - 188).abs() <= 1, "{}", b[1]); assert_eq!(b[2], 0); @@ -278,7 +289,8 @@ mod tests { set.tile_resolutions.insert(1002, 64); let comp = Composite { block_scales: set.block_scales(), ..Composite::new(&[], 512) }; let alpha = |tx: u32| tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Extra, tx, 0)[3]; - // Tile 1001 fills its block; 1002 (from texel 256) a quarter of it: 32 × log2(4). + + // Tile 1001 fills its block; 1002 fills a quarter, encoded as 32 × log2(4) assert_eq!((alpha(0), alpha(3), alpha(4), alpha(7)), (0, 0, 64, 64)); } @@ -286,16 +298,18 @@ mod tests { fn missing_channels_fall_back_to_defaults() { let comp = Composite::new(&[], 64); let orm = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Orm, 0, 0); + assert_eq!(&orm[..4], &[255, 128, 0, 128]); - let n = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Normal, 0, 0); - assert_eq!(&n[..4], &[128, 128, 255, 128], "sheen roughness 0.5 rides in alpha"); + assert_eq!(normal_texel(&comp, &BakedMaps::default(), 0), [0.0, 0.0, 1.0, 0.5], "flat, sheen roughness 0.5 rides in alpha"); assert_eq!(&tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Emissive, 0, 0)[..4], &[0, 0, 0, 0]); assert_eq!(&tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Lobes, 0, 0)[..4], &[0, 25, 0, 0], "lobes off, coat roughness 0.1"); assert_eq!(&tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Sheen, 0, 0)[..4], &[255, 255, 255, 0]); assert_eq!(&tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Extra, 0, 0)[..4], &[128, 128, 0, 0], "flat coat normal, no transmission"); + for k in DisplayKind::ALL { assert_eq!(k.default_texel(), tile_bytes(&comp, &BakedMaps::default(), k, 3, 5)[..k.bytes_per_texel()]); } + let h = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Height, 0, 0); assert_eq!(h.len(), TILE_AREA * 8); assert_eq!(half::f16::from_le_bytes([h[2], h[3]]).to_f32(), half::f16::from_f32(0.04).to_f32(), "default F0"); @@ -303,30 +317,70 @@ mod tests { #[test] fn the_baked_normal_is_the_base_of_the_shown_normal() { - // A baked normal tilted to +X (packed), 64² like the set. + // A baked normal tilted to +X at the set resolution let tilted = [0.8f32, 0.5, 0.9]; let baked = BakedMaps { resolution: 64, maps: [(BakedMap::Normal, std::sync::Arc::new(tilted.repeat(64 * 64)))].into_iter().collect() }; - let shown = |comp: &Composite| tile_bytes(comp, &baked, DisplayKind::Normal, 0, 0)[..3].to_vec(); - let pack = |v: [f32; 3]| v.map(|x| (x * 255.0 + 0.5) as u8).to_vec(); - // No normal channel: the baked normal as it is. + let shown = |comp: &Composite| normal_texel(comp, &baked, 0)[..3].to_vec(); + let pack = |v: [f32; 3]| v.map(|x| half::f16::from_f32(x * 2.0 - 1.0).to_f32()).to_vec(); + + // Without a painted normal, show the bake directly assert_eq!(shown(&Composite::new(&[], 64)), pack(tilted)); - // A flat painted normal leaves it; a painted detail is re-oriented onto it. + + // Flat paint preserves the bake; tilted paint reorients onto it let mut comp = Composite::new(&[Channel::Normal], 64); comp.channels.get_mut(&Channel::Normal).unwrap().set(0, 0, &[to_u16(0.5), to_u16(0.5), to_u16(1.0)]); - assert!(shown(&comp).iter().zip(pack(tilted)).all(|(a, b)| (*a as i32 - b as i32).abs() <= 1), "{:?}", shown(&comp)); + + assert!(shown(&comp).iter().zip(pack(tilted)).all(|(a, b)| (a - b).abs() < 1e-3), "{:?}", shown(&comp)); + comp.channels.get_mut(&Channel::Normal).unwrap().set(0, 0, &[to_u16(0.5), to_u16(0.8), to_u16(0.9)]); + assert_eq!(shown(&comp), pack(texturecraft_core::blend::rnm(tilted, [0.5, 0.8, 0.9]))); } + /// Decode texel `i` of the Normal display tile (0, 0). + fn normal_texel(comp: &Composite, baked: &BakedMaps, i: usize) -> [f32; 4] { + let bytes = tile_bytes(comp, baked, DisplayKind::Normal, 0, 0); + [0, 1, 2, 3].map(|k| half::f16::from_le_bytes([bytes[i * 8 + k * 2], bytes[i * 8 + k * 2 + 1]]).to_f32()) + } + + #[test] + fn baked_normals_keep_more_than_8_bits_in_the_viewport() { + // Steps of 1/1024 distinguish texels that an 8-bit texture merges + let ramp: Vec = (0..64 * 64).flat_map(|i| [0.5 + (i % 64) as f32 / 1024.0, 0.5, 0.9]).collect(); + let baked = BakedMaps { resolution: 64, maps: [(BakedMap::Normal, std::sync::Arc::new(ramp))].into_iter().collect() }; + let comp = Composite::new(&[], 64); + + let xs: Vec = (0..8).map(|i| normal_texel(&comp, &baked, i)[0]).collect(); + + assert!(xs.windows(2).all(|w| w[1] > w[0]), "every texel of the gradient is distinct: {xs:?}"); + assert!(xs.windows(2).all(|w| w[1] - w[0] < 1.0 / 255.0), "steps finer than one 8-bit level: {xs:?}"); + } + + #[test] + fn nonfinite_baked_normal_does_not_reach_the_display_texture() { + let mut pixels = vec![0.5; 64 * 64 * 3]; + pixels[..3].copy_from_slice(&[f32::NAN, 0.5, 1.0]); + pixels[3..6].copy_from_slice(&[0.5, f32::INFINITY, 1.0]); + let baked = BakedMaps { resolution: 64, maps: [(BakedMap::Normal, std::sync::Arc::new(pixels))].into_iter().collect() }; + let composite = Composite::new(&[], 64); + + assert_eq!(normal_texel(&composite, &baked, 0), [0.0, 0.0, 1.0, 0.5]); + assert_eq!(normal_texel(&composite, &baked, 1), [0.0, 0.0, 1.0, 0.5]); + } + #[test] fn glossiness_shows_as_roughness_and_specular_rides_in_height() { let mut comp = Composite::new(&[Channel::Glossiness, Channel::Specular], 64); comp.channels.get_mut(&Channel::Glossiness).unwrap().set(0, 0, &[to_u16(0.8)]); comp.channels.get_mut(&Channel::Specular).unwrap().set(0, 0, &[to_u16(1.0), to_u16(0.5), 0]); + let orm = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Orm, 0, 0); + assert!((orm[1] as i32 - 51).abs() <= 1, "roughness = 1 - 0.8, got {}", orm[1]); + let h = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Height, 0, 0); let at = |k: usize| half::f16::from_le_bytes([h[k * 2], h[k * 2 + 1]]).to_f32(); + assert_eq!(at(0), 0.0); assert!((at(1) - 1.0).abs() < 1e-3 && (at(2) - 0.5).abs() < 1e-3 && at(3) == 0.0); assert_eq!(DisplayKind::of_channel(Channel::Specular), DisplayKind::Height); @@ -347,11 +401,13 @@ mod tests { set(Channel::ScatterWeight, &[to_u16(0.4)]); set(Channel::Transmission, &[to_u16(0.6)]); set(Channel::CoatNormal, &[to_u16(1.0), to_u16(0.5), to_u16(1.0)]); + let px = |k: DisplayKind| tile_bytes(&comp, &BakedMaps::default(), k, 0, 0)[4..8].to_vec(); let near = |a: &[u8], b: &[u8]| a.iter().zip(b).all(|(x, y)| x.abs_diff(*y) <= 1); + assert!(near(&px(DisplayKind::Lobes), &[255, 51, 153, 64]), "{:?}", px(DisplayKind::Lobes)); assert!(near(&px(DisplayKind::Sheen), &[255, 0, 188, 204]), "{:?}", px(DisplayKind::Sheen)); - assert!(near(&px(DisplayKind::Normal)[3..], &[77]) && near(&px(DisplayKind::Emissive)[3..], &[102])); + assert!((normal_texel(&comp, &BakedMaps::default(), 1)[3] - 0.3).abs() < 1e-3 && near(&px(DisplayKind::Emissive)[3..], &[102])); assert!(near(&px(DisplayKind::Extra), &[255, 128, 153, 0]), "{:?}", px(DisplayKind::Extra)); for c in Channel::LOBES { assert_ne!(DisplayKind::of_channel(c), DisplayKind::Orm, "{c:?}"); diff --git a/crates/compose/src/eval.rs b/crates/compose/src/eval.rs index e38aac84..7bea1c3d 100644 --- a/crates/compose/src/eval.rs +++ b/crates/compose/src/eval.rs @@ -327,6 +327,22 @@ fn neighbourhood_filters(effects: &[Effect], res: u32) -> impl Iterator