Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error in clip reduction #659

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/scenes/src/test_scenes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export_scenes!(
brush_transform(brush_transform: animated),
blend_grid(blend_grid),
deep_blend(deep_blend),
many_clips(many_clips),
conflation_artifacts(conflation_artifacts),
labyrinth(labyrinth),
robust_paths(robust_paths),
Expand All @@ -82,7 +83,11 @@ export_scenes!(
/// Implementations for the test scenes.
/// In a module because the exported [`ExampleScene`](crate::ExampleScene) creation functions use the same names.
mod impls {
use std::f64::consts::PI;

use crate::SceneParams;
use rand::Rng;
use rand::{rngs::StdRng, SeedableRng};
use vello::kurbo::{
Affine, BezPath, Cap, Circle, Ellipse, Join, PathEl, Point, Rect, Shape, Stroke, Vec2,
};
Expand Down Expand Up @@ -1094,6 +1099,32 @@ mod impls {
}
}

pub(super) fn many_clips(scene: &mut Scene, params: &mut SceneParams) {
params.resolution = Some(Vec2::new(1000., 1000.));
let mut rng = StdRng::seed_from_u64(42);
let mut base_tri = BezPath::new();
base_tri.move_to((-50.0, 0.0));
base_tri.line_to((25.0, -43.3));
base_tri.line_to((25.0, 43.3));
for y in 0..10 {
for x in 0..10 {
let translate =
Affine::translate((100. * (x as f64 + 0.5), 100. * (y as f64 + 0.5)));
const CLIPS_PER_FILL: usize = 3;
for _ in 0..CLIPS_PER_FILL {
let rot = Affine::rotate(rng.gen_range(0.0..PI));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the root path is rotationally symmetric every $$\frac{2\pi}{3}$$ radians, but this is doing a rotation every $$\pi$$ radians.

I think that means there's a bias towards the range which is equivalent to $$[0, \frac{\pi}{3})$$.

Practically, I don't think this matters here, but I'm wondering if this is an intentional choice.

(And of course, my maths understanding could be very wrong :P)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, you're right, there is a bias. Not thinking it through carefully, I assumed the symmetry would make it still uniform, and I wanted to make the code concise. I think I'll leave it, in the sprit of kintsugi.

Copy link
Member

@DJMcNab DJMcNab Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use $$\tau$$ (TAU) here to still be fairly concise.

But practically, this is great, and the bias isn't an issue!

scene.push_layer(Mix::Clip, 1.0, translate * rot, &base_tri);
}
let rot = Affine::rotate(rng.gen_range(0.0..PI));
let color = Color::rgb(rng.gen(), rng.gen(), rng.gen());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This produces remarkably pleasing colours considering that it's completely random - it's probably a good technique to use in other examples.

scene.fill(Fill::NonZero, translate * rot, color, None, &base_tri);
for _ in 0..CLIPS_PER_FILL {
scene.pop_layer();
}
}
}
}

// Support functions

pub(super) fn render_cardioid(scene: &mut Scene) {
Expand Down
3 changes: 3 additions & 0 deletions vello_shaders/shader/clip_reduce.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ fn main(
workgroupBarrier();
let size = sh_bic[0].b;
bic = Bic();
if local_id.x + 1u < WG_SIZE {
bic = sh_bic[local_id.x + 1u];
}
if is_push && bic.a == 0u {
let local_ix = size - bic.b - 1u;
sh_parent[local_ix] = local_id.x;
Expand Down
2 changes: 1 addition & 1 deletion vello_shaders/src/cpu/clip_reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ fn clip_reduce_main(
let inp = clip_inp[global_ix].path_ix;
let is_push = inp >= 0;
let bic = ClipBic::new(1 - is_push as u32, is_push as u32);
bic_reduced = bic.combine(bic_reduced);
if is_push && bic_reduced.a == 0 {
scratch.push(global_ix as u32);
}
bic_reduced = bic.combine(bic_reduced);
}
reduced[wg_ix as usize] = bic_reduced;
for (i, parent_ix) in scratch.iter().rev().enumerate() {
Expand Down
3 changes: 3 additions & 0 deletions vello_tests/snapshots/many_clips.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions vello_tests/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ fn snapshot_deep_blend() {
let params = TestParams::new("deep_blend", 200, 200);
snapshot_test_scene(test_scene, params);
}

#[test]
#[cfg_attr(skip_gpu_tests, ignore)]
fn snapshot_many_clips() {
let test_scene = test_scenes::many_clips();
let params = TestParams::new("many_clips", 200, 200);
snapshot_test_scene(test_scene, params);
}