mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-25 17:00:50 +01:00
Levels histogram: sample every column, and read masks of empty layers
An odd tile stride: a power of two divides the row length on power-of-two sets and read the same few columns. A mask is planned on its own, so a paint layer with nothing painted still shows its mask's histogram.
This commit is contained in:
1 parent
d736ffa9af
commit
83b8a13131
2 files changed
+37
-11
No files matched your search
+31
-11
@@ -1849,6 +1849,15 @@ pub fn mask_image(inp: &Inputs, id: LayerId, size: u32) -> Option<Vec<u8>> {
|
||||
/// Tiles [`effect_histogram`] samples at most (a million texels).
|
||||
const HISTOGRAM_TILES: usize = 256;
|
||||
|
||||
/// The tiles [`effect_histogram`] reads: an even sample of at most [`HISTOGRAM_TILES`], which
|
||||
/// keeps the cost flat up to 8K. The stride is odd: a power of two would divide the row length
|
||||
/// and sample the same few columns.
|
||||
fn histogram_tiles(width: u32, height: u32) -> Vec<(u32, u32)> {
|
||||
let all = all_tiles(width, height);
|
||||
let stride = all.len().div_ceil(HISTOGRAM_TILES).max(1) | 1;
|
||||
all.into_iter().step_by(stride).collect()
|
||||
}
|
||||
|
||||
/// Histogram (`bins` buckets over 0..1) of the values entering effect `effect`: its layer's mask
|
||||
/// below it, or (a content effect) the layer's `channel` below it where the layer has content,
|
||||
/// color components pooled and signed values mapped to 0..1. Over the texels the mesh covers
|
||||
@@ -1874,31 +1883,31 @@ pub fn effect_histogram(inp: &Inputs, effect: LayerId, channel: Channel, bins: u
|
||||
};
|
||||
let cut_inp = Inputs { project: &cut, preview: None, ..*inp };
|
||||
let channels = cut.texture_set(inp.ts).map(|t| t.channel_list()).unwrap_or_default();
|
||||
// An even sample of at most HISTOGRAM_TILES tiles keeps the cost flat up to 8K.
|
||||
let all = all_tiles(inp.gbuf.width, inp.gbuf.height);
|
||||
let tiles: Vec<(u32, u32)> = all.iter().copied().step_by(all.len().div_ceil(HISTOGRAM_TILES).max(1)).collect();
|
||||
let tiles = histogram_tiles(inp.gbuf.width, inp.gbuf.height);
|
||||
let fields = neighbourhood_fields(&cut_inp, &channels, &tiles);
|
||||
let bin = |v: f32| ((v.clamp(0.0, 1.0) * bins as f32) as usize).min(bins - 1);
|
||||
let hist = tiles
|
||||
.par_iter()
|
||||
.map(|&(tx, ty)| {
|
||||
let mut h = vec![0u32; bins];
|
||||
let nodes = plan(&cut_inp, &[owner], &channels, tx, ty, 0, Some(&fields));
|
||||
let Some(n) = nodes.first() else { return h };
|
||||
let c = n.chans.iter().find(|c| Channel::ALL.get(c.ch) == Some(&channel));
|
||||
// The mask is planned on its own: a layer with nothing drawn plans no node.
|
||||
let mask = if in_mask { layer_mask(&cut, owner).and_then(|m| mask_plan(&cut_inp, owner, m, tx, ty, Some(&fields))) } else { None };
|
||||
let nodes = if in_mask { Vec::new() } else { plan(&cut_inp, &[owner], &channels, tx, ty, 0, Some(&fields)) };
|
||||
let content = nodes.first().and_then(|n| n.chans.iter().find(|c| Channel::ALL.get(c.ch) == Some(&channel)).map(|c| (n, c)));
|
||||
if mask.is_none() && content.is_none() {
|
||||
return h;
|
||||
}
|
||||
let full = anchor_plan(&cut_inp, owner, &channels, tx, ty, Some(&fields));
|
||||
for (x, y, o) in tile_texels(inp.gbuf, tx, ty) {
|
||||
let mut tex = Texel::new(&cut_inp, x, y, o, 0.0);
|
||||
if !full.is_empty() {
|
||||
tex.anchors_below(&full, owner);
|
||||
}
|
||||
if in_mask {
|
||||
if let Some(m) = &n.mask
|
||||
&& let Some(b) = h.get_mut(bin(tex.mask(m)))
|
||||
{
|
||||
if let Some(m) = &mask {
|
||||
if let Some(b) = h.get_mut(bin(tex.mask(m))) {
|
||||
*b += 1;
|
||||
}
|
||||
} else if let Some(c) = c
|
||||
} else if let Some((n, c)) = content
|
||||
&& let Some((v, _)) = tex.content(&n.content, c)
|
||||
{
|
||||
// As levels reads them (`per_unit`); normal maps pass through levels unchanged.
|
||||
@@ -2037,6 +2046,17 @@ mod tests {
|
||||
(0..3).all(|i| (a[i] - b[i]).abs() < 2e-4)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn histogram_samples_spread_over_every_column() {
|
||||
assert_eq!(histogram_tiles(1024, 1024), all_tiles(1024, 1024), "small sets are read whole");
|
||||
for size in [2048, 4096, 8192] {
|
||||
let tiles = histogram_tiles(size, size);
|
||||
let columns: std::collections::BTreeSet<u32> = tiles.iter().map(|t| t.0).collect();
|
||||
assert!(tiles.len() <= HISTOGRAM_TILES, "{size}: {} tiles", tiles.len());
|
||||
assert!(columns.len() as u32 >= size / TILE / 2, "{size}: only {} columns", columns.len());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_stack_gives_channel_defaults() {
|
||||
let f = fixture();
|
||||
|
||||
@@ -140,6 +140,12 @@ fn histogram_counts_what_an_effect_reads() {
|
||||
assert_eq!(target, "mask");
|
||||
assert!(one(&bins, 1), "mask 0.3: {bins:?}");
|
||||
assert_eq!(hist(&mut s, json!({"effect": ml})).1.len(), 64);
|
||||
// A paint layer with nothing painted yet still has its mask.
|
||||
execute(&mut s, "layer.add.paint", &Value::Null).unwrap();
|
||||
execute(&mut s, "mask.add.black", &Value::Null).unwrap();
|
||||
execute(&mut s, "effect.add.fill", &json!({"target": "mask", "value": 0.6})).unwrap();
|
||||
let pl = id(execute(&mut s, "effect.add.levels", &json!({"target": "mask"})).unwrap(), "effect");
|
||||
assert!(one(&hist(&mut s, json!({"effect": pl, "bins": 4})).1, 2), "an empty paint layer's mask");
|
||||
assert!(execute(&mut s, "effect.histogram", &json!({"effect": 999_999})).is_err());
|
||||
assert!(execute(&mut s, "effect.histogram", &json!({"effect": lv, "channel": "nope"})).is_err());
|
||||
}
|
||||
Reference in new issue
Block a user