Implement the Brush tool (#1099)

* Implement Brush Node

* Add color Input

* Add VectorPointsNode

* Add Erase Node

* Adapt compilation infrastructure to allow non Image Frame inputs

* Remove debug output from TransformNode

* Fix transform calculation

* Fix Blending by making the brush texture use associated alpha

* Code improvements and UX polish

* Rename Opacity to Flow

* Add erase option to brush node + fix freehand tool

* Fix crash

* Revert erase implementation

* Fix flattening id calculation

* Fix some transformation issues

* Fix changing the pivot location

* Fix vector data modify bounds

* Minor fn name cleanup

* Fix some tests

* Fix tests

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
Co-authored-by: hypercube <0hypercube@gmail.com>
This commit is contained in:
Dennis Kobert 2023-04-11 10:35:21 +02:00 committed by Keavon Chambers
parent 758f757775
commit 589ff9a2d3
36 changed files with 1527 additions and 406 deletions

View file

@ -12,7 +12,7 @@ license = "MIT OR Apache-2.0"
std = ["dyn-any", "dyn-any/std", "alloc", "glam/std", "specta"]
default = ["async", "serde", "kurbo", "log", "std"]
log = ["dep:log"]
serde = ["dep:serde", "glam/serde"]
serde = ["dep:serde", "glam/serde", "bezier-rs/serde"]
gpu = ["spirv-std", "bytemuck", "glam/bytemuck", "dyn-any", "glam/libm"]
async = ["async-trait", "alloc"]
nightly = []
@ -20,13 +20,18 @@ alloc = ["dyn-any", "bezier-rs", "once_cell"]
type_id_logging = []
[dependencies]
dyn-any = {path = "../../libraries/dyn-any", features = ["derive", "glam"], optional = true, default-features = false }
dyn-any = { path = "../../libraries/dyn-any", features = [
"derive",
"glam",
], optional = true, default-features = false }
spirv-std = { version = "0.5", features = ["glam"] , optional = true}
bytemuck = {version = "1.8", features = ["derive"], optional = true}
async-trait = {version = "0.1", optional = true}
serde = {version = "1.0", features = ["derive"], optional = true, default-features = false }
log = {version = "0.4", optional = true}
spirv-std = { version = "0.5", features = ["glam"], optional = true }
bytemuck = { version = "1.8", features = ["derive"], optional = true }
async-trait = { version = "0.1", optional = true }
serde = { version = "1.0", features = [
"derive",
], optional = true, default-features = false }
log = { version = "0.4", optional = true }
bezier-rs = { path = "../../libraries/bezier-rs", optional = true }
kurbo = { git = "https://github.com/linebender/kurbo.git", features = [
@ -34,8 +39,10 @@ kurbo = { git = "https://github.com/linebender/kurbo.git", features = [
], optional = true }
rand_chacha = "0.3.1"
spin = "0.9.2"
glam = { version = "^0.22", default-features = false, features = ["scalar-math"]}
node-macro = {path = "../node-macro"}
glam = { version = "^0.22", default-features = false, features = [
"scalar-math",
] }
node-macro = { path = "../node-macro" }
specta.workspace = true
specta.optional = true
once_cell = { version = "1.17.0", default-features = false, optional = true }

View file

@ -60,7 +60,7 @@ where
core::any::type_name::<Self::Output>()
}
#[cfg(feature = "alloc")]
fn to_node_io(&self, parameters: Vec<(Type, Type)>) -> NodeIOTypes {
fn to_node_io(&self, parameters: Vec<Type>) -> NodeIOTypes {
NodeIOTypes {
input: concrete!(<Input as StaticType>::Static),
output: concrete!(<Self::Output as StaticType>::Static),

View file

@ -23,7 +23,7 @@ pub struct AddParameterNode<Second> {
}
#[node_macro::node_fn(AddParameterNode)]
fn flat_map<U, T>(first: U, second: T) -> <U as Add<T>>::Output
fn add_parameter<U, T>(first: U, second: T) -> <U as Add<T>>::Output
where
U: Add<T>,
{

View file

@ -239,12 +239,11 @@ fn brighten_color_node(color: Color, brightness: f32) -> Color {
}
#[derive(Debug)]
pub struct ForEachNode<Iter, MapNode> {
pub struct ForEachNode<MapNode> {
map_node: MapNode,
_iter: PhantomData<Iter>,
}
#[node_macro::node_fn(ForEachNode<_Iter>)]
#[node_macro::node_fn(ForEachNode)]
fn map_node<_Iter: Iterator, MapNode>(input: _Iter, map_node: &'any_input MapNode) -> ()
where
MapNode: for<'any_input> Node<'any_input, _Iter::Item, Output = ()> + 'input,
@ -359,6 +358,15 @@ mod image {
data: Vec::new(),
}
}
pub fn new(width: u32, height: u32, color: Color) -> Self {
Self {
width,
height,
data: vec![color; (width * height) as usize],
}
}
pub fn as_slice(&self) -> ImageSlice {
ImageSlice {
width: self.width,
@ -366,6 +374,15 @@ mod image {
data: self.data.as_slice(),
}
}
pub fn get_mut(&mut self, x: u32, y: u32) -> Option<&mut Color> {
self.data.get_mut((y * self.width + x) as usize)
}
pub fn get(&self, x: u32, y: u32) -> Option<&Color> {
self.data.get((y * self.width + x) as usize)
}
/// Generate Image from some frontend image data (the canvas pixels as u8s in a flat array)
pub fn from_image_data(image_data: &[u8], width: u32, height: u32) -> Self {
let data = image_data.chunks_exact(4).map(|v| Color::from_rgba8(v[0], v[1], v[2], v[3])).collect();
@ -451,6 +468,12 @@ mod image {
}
}
impl AsRef<ImageFrame> for ImageFrame {
fn as_ref(&self) -> &ImageFrame {
self
}
}
impl Hash for ImageFrame {
fn hash<H: Hasher>(&self, state: &mut H) {
self.image.hash(state);

View file

@ -300,7 +300,7 @@ pub struct InvertRGBNode;
#[node_macro::node_fn(InvertRGBNode)]
fn invert_image(color: Color) -> Color {
color.map_rgb(|c| 1. - c)
color.map_rgb(|c| color.a() - c)
}
#[derive(Debug, Clone, Copy)]
@ -339,43 +339,55 @@ pub struct BlendNode<BlendMode, Opacity> {
opacity: Opacity,
}
impl<Opacity: dyn_any::StaticTypeSized, Blend: dyn_any::StaticTypeSized> StaticType for BlendNode<Blend, Opacity> {
type Static = BlendNode<Blend::Static, Opacity::Static>;
}
#[node_macro::node_fn(BlendNode)]
fn blend_node(input: (Color, Color), blend_mode: BlendMode, opacity: f64) -> Color {
let (source_color, backdrop) = input;
let actual_opacity = 1. - (opacity / 100.) as f32;
return match blend_mode {
BlendMode::Normal => backdrop.blend_rgb(source_color, Color::blend_normal),
BlendMode::Multiply => backdrop.blend_rgb(source_color, Color::blend_multiply),
BlendMode::Darken => backdrop.blend_rgb(source_color, Color::blend_darken),
BlendMode::ColorBurn => backdrop.blend_rgb(source_color, Color::blend_color_burn),
BlendMode::LinearBurn => backdrop.blend_rgb(source_color, Color::blend_linear_burn),
BlendMode::DarkerColor => backdrop.blend_darker_color(source_color),
let opacity = opacity / 100.;
BlendMode::Screen => backdrop.blend_rgb(source_color, Color::blend_screen),
BlendMode::Lighten => backdrop.blend_rgb(source_color, Color::blend_lighten),
BlendMode::ColorDodge => backdrop.blend_rgb(source_color, Color::blend_color_dodge),
BlendMode::LinearDodge => backdrop.blend_rgb(source_color, Color::blend_linear_dodge),
BlendMode::LighterColor => backdrop.blend_lighter_color(source_color),
let (foreground, background) = input;
let foreground = foreground.to_linear_srgb();
let background = background.to_linear_srgb();
BlendMode::Overlay => source_color.blend_rgb(backdrop, Color::blend_hardlight),
BlendMode::SoftLight => backdrop.blend_rgb(source_color, Color::blend_softlight),
BlendMode::HardLight => backdrop.blend_rgb(source_color, Color::blend_hardlight),
BlendMode::VividLight => backdrop.blend_rgb(source_color, Color::blend_vivid_light),
BlendMode::LinearLight => backdrop.blend_rgb(source_color, Color::blend_linear_light),
BlendMode::PinLight => backdrop.blend_rgb(source_color, Color::blend_pin_light),
BlendMode::HardMix => backdrop.blend_rgb(source_color, Color::blend_hard_mix),
let target_color = match blend_mode {
BlendMode::Normal => background.blend_rgb(foreground, Color::blend_normal),
BlendMode::Multiply => background.blend_rgb(foreground, Color::blend_multiply),
BlendMode::Darken => background.blend_rgb(foreground, Color::blend_darken),
BlendMode::ColorBurn => background.blend_rgb(foreground, Color::blend_color_burn),
BlendMode::LinearBurn => background.blend_rgb(foreground, Color::blend_linear_burn),
BlendMode::DarkerColor => background.blend_darker_color(foreground),
BlendMode::Difference => backdrop.blend_rgb(source_color, Color::blend_exclusion),
BlendMode::Exclusion => backdrop.blend_rgb(source_color, Color::blend_exclusion),
BlendMode::Subtract => backdrop.blend_rgb(source_color, Color::blend_subtract),
BlendMode::Divide => backdrop.blend_rgb(source_color, Color::blend_divide),
BlendMode::Screen => background.blend_rgb(foreground, Color::blend_screen),
BlendMode::Lighten => background.blend_rgb(foreground, Color::blend_lighten),
BlendMode::ColorDodge => background.blend_rgb(foreground, Color::blend_color_dodge),
BlendMode::LinearDodge => background.blend_rgb(foreground, Color::blend_linear_dodge),
BlendMode::LighterColor => background.blend_lighter_color(foreground),
BlendMode::Hue => backdrop.blend_hue(source_color),
BlendMode::Saturation => backdrop.blend_saturation(source_color),
BlendMode::Color => backdrop.blend_color(source_color),
BlendMode::Luminosity => backdrop.blend_luminosity(source_color),
}
.lerp(backdrop, actual_opacity);
BlendMode::Overlay => foreground.blend_rgb(background, Color::blend_hardlight),
BlendMode::SoftLight => background.blend_rgb(foreground, Color::blend_softlight),
BlendMode::HardLight => background.blend_rgb(foreground, Color::blend_hardlight),
BlendMode::VividLight => background.blend_rgb(foreground, Color::blend_vivid_light),
BlendMode::LinearLight => background.blend_rgb(foreground, Color::blend_linear_light),
BlendMode::PinLight => background.blend_rgb(foreground, Color::blend_pin_light),
BlendMode::HardMix => background.blend_rgb(foreground, Color::blend_hard_mix),
BlendMode::Difference => background.blend_rgb(foreground, Color::blend_exclusion),
BlendMode::Exclusion => background.blend_rgb(foreground, Color::blend_exclusion),
BlendMode::Subtract => background.blend_rgb(foreground, Color::blend_subtract),
BlendMode::Divide => background.blend_rgb(foreground, Color::blend_divide),
BlendMode::Hue => background.blend_hue(foreground),
BlendMode::Saturation => background.blend_saturation(foreground),
BlendMode::Color => background.blend_color(foreground),
BlendMode::Luminosity => background.blend_luminosity(foreground),
};
let multiplied_target_color = target_color.to_associated_alpha(opacity as f32);
let blended = background.alpha_blend(multiplied_target_color);
blended.to_gamma_srgb()
}
#[derive(Debug, Clone, Copy)]

View file

@ -83,6 +83,11 @@ impl Color {
Color { red, green, blue, alpha }
}
/// Return an opaque `Color` from given `f32` RGB channels.
pub fn from_unassociated_alpha(red: f32, green: f32, blue: f32, alpha: f32) -> Color {
Color::from_rgbaf32_unchecked(red * alpha, green * alpha, blue * alpha, alpha)
}
/// Return an opaque SDR `Color` given RGB channels from `0` to `255`.
///
/// # Examples
@ -602,14 +607,49 @@ impl Color {
pub fn map_rgb<F: Fn(f32) -> f32>(&self, f: F) -> Self {
Self::from_rgbaf32_unchecked(f(self.r()), f(self.g()), f(self.b()), self.a())
}
pub fn blend_rgb<F: Fn(f32, f32) -> f32>(&self, other: Color, f: F) -> Self {
Color {
red: f(self.red, other.red).clamp(0., 1.),
green: f(self.green, other.green).clamp(0., 1.),
blue: f(self.blue, other.blue).clamp(0., 1.),
pub fn apply_opacity(&self, opacity: f32) -> Self {
Self::from_rgbaf32_unchecked(self.r(), self.g(), self.b(), self.a() * opacity)
}
pub fn to_associated_alpha(&self, alpha: f32) -> Self {
Self {
red: self.red * alpha,
green: self.green * alpha,
blue: self.blue * alpha,
alpha: self.alpha * alpha,
}
}
pub fn to_unassociated_alpha(&self) -> Self {
let unmultiply = 1. / self.alpha;
Self {
red: self.red * unmultiply,
green: self.green * unmultiply,
blue: self.blue * unmultiply,
alpha: self.alpha,
}
}
pub fn blend_rgb<F: Fn(f32, f32) -> f32>(&self, other: Color, f: F) -> Self {
let background = self.to_unassociated_alpha();
Color {
red: f(background.red, other.red).clamp(0., 1.),
green: f(background.green, other.green).clamp(0., 1.),
blue: f(background.blue, other.blue).clamp(0., 1.),
alpha: other.alpha,
}
}
pub fn alpha_blend(&self, other: Color) -> Self {
let inv_alpha = 1. - other.alpha;
Self {
red: self.red * inv_alpha + other.red,
green: self.green * inv_alpha + other.green,
blue: self.blue * inv_alpha + other.blue,
alpha: self.alpha * inv_alpha + other.alpha,
}
}
}
#[test]

View file

@ -6,6 +6,61 @@ use crate::raster::ImageFrame;
use crate::vector::VectorData;
use crate::Node;
pub trait Transform {
fn transform(&self) -> DAffine2;
fn local_pivot(&self, pivot: DVec2) -> DVec2 {
pivot
}
}
pub trait TransformMut: Transform {
fn transform_mut(&mut self) -> &mut DAffine2;
fn translate(&mut self, offset: DVec2) {
*self.transform_mut() = DAffine2::from_translation(offset) * self.transform();
}
}
impl Transform for ImageFrame {
fn transform(&self) -> DAffine2 {
self.transform
}
}
impl Transform for &ImageFrame {
fn transform(&self) -> DAffine2 {
self.transform
}
}
impl TransformMut for ImageFrame {
fn transform_mut(&mut self) -> &mut DAffine2 {
&mut self.transform
}
}
impl Transform for VectorData {
fn transform(&self) -> DAffine2 {
self.transform
}
fn local_pivot(&self, pivot: DVec2) -> DVec2 {
self.local_pivot(pivot)
}
}
impl TransformMut for VectorData {
fn transform_mut(&mut self) -> &mut DAffine2 {
&mut self.transform
}
}
impl Transform for DAffine2 {
fn transform(&self) -> DAffine2 {
*self
}
}
impl TransformMut for DAffine2 {
fn transform_mut(&mut self) -> &mut DAffine2 {
self
}
}
#[derive(Debug, Clone, Copy)]
pub struct TransformNode<Translation, Rotation, Scale, Shear, Pivot> {
pub(crate) translate: Translation,
@ -16,45 +71,12 @@ pub struct TransformNode<Translation, Rotation, Scale, Shear, Pivot> {
}
#[node_macro::node_fn(TransformNode)]
pub(crate) fn transform_vector_data(mut vector_data: VectorData, translate: DVec2, rotate: f64, scale: DVec2, shear: DVec2, pivot: DVec2) -> VectorData {
let pivot = DAffine2::from_translation(vector_data.local_pivot(pivot));
pub(crate) fn transform_vector_data<Data: TransformMut>(mut data: Data, translate: DVec2, rotate: f64, scale: DVec2, shear: DVec2, pivot: DVec2) -> Data {
let pivot = DAffine2::from_translation(data.local_pivot(pivot));
let modification = DAffine2::from_scale_angle_translation(scale, rotate, translate) * DAffine2::from_cols_array(&[1., shear.y, shear.x, 1., 0., 0.]) * pivot.inverse();
vector_data.transform = modification * vector_data.transform;
let modification = pivot * DAffine2::from_scale_angle_translation(scale, rotate, translate) * DAffine2::from_cols_array(&[1., shear.y, shear.x, 1., 0., 0.]) * pivot.inverse();
let data_transform = data.transform_mut();
*data_transform = modification * (*data_transform);
vector_data
}
impl<'input, Translation: 'input, Rotation: 'input, Scale: 'input, Shear: 'input, Pivot: 'input> Node<'input, ImageFrame> for TransformNode<Translation, Rotation, Scale, Shear, Pivot>
where
Translation: for<'any_input> Node<'any_input, (), Output = DVec2>,
Rotation: for<'any_input> Node<'any_input, (), Output = f64>,
Scale: for<'any_input> Node<'any_input, (), Output = DVec2>,
Shear: for<'any_input> Node<'any_input, (), Output = DVec2>,
Pivot: for<'any_input> Node<'any_input, (), Output = DVec2>,
{
type Output = ImageFrame;
#[inline]
fn eval<'node: 'input>(&'node self, mut image_frame: ImageFrame) -> Self::Output {
let translate = self.translate.eval(());
let rotate = self.rotate.eval(());
let scale = self.scale.eval(());
let shear = self.shear.eval(());
let pivot = self.pivot.eval(());
let pivot = DAffine2::from_translation(pivot);
let modification = pivot * DAffine2::from_scale_angle_translation(scale, rotate, translate) * DAffine2::from_cols_array(&[1., shear.y, shear.x, 1., 0., 0.]) * pivot.inverse();
image_frame.transform = modification * image_frame.transform;
image_frame
}
}
// Generates a transform matrix that rotates around the center of the image
fn generate_transform(shear: DVec2, transform: &DAffine2, scale: DVec2, rotate: f64, translate: DVec2) -> DAffine2 {
let shear_matrix = DAffine2::from_cols_array(&[1., shear.y, shear.x, 1., 0., 0.]);
let pivot = transform.transform_point2(DVec2::splat(0.5));
let translate_to_center = DAffine2::from_translation(-pivot);
translate_to_center.inverse() * DAffine2::from_scale_angle_translation(scale, rotate, translate) * shear_matrix * translate_to_center
data
}

View file

@ -9,13 +9,17 @@ pub use std::borrow::Cow;
pub struct NodeIOTypes {
pub input: Type,
pub output: Type,
pub parameters: Vec<(Type, Type)>,
pub parameters: Vec<Type>,
}
impl NodeIOTypes {
pub fn new(input: Type, output: Type, parameters: Vec<(Type, Type)>) -> Self {
pub fn new(input: Type, output: Type, parameters: Vec<Type>) -> Self {
Self { input, output, parameters }
}
pub fn ty(&self) -> Type {
Type::Fn(Box::new(self.input.clone()), Box::new(self.output.clone()))
}
}
#[macro_export]
@ -34,6 +38,20 @@ macro_rules! generic {
}};
}
#[macro_export]
macro_rules! fn_type {
($input:ty, $output:ty) => {
Type::Fn(Box::new(concrete!($input)), Box::new(concrete!($output)))
};
}
#[macro_export]
macro_rules! value_fn {
($output:ty) => {
Type::Fn(Box::new(concrete!(())), Box::new(concrete!($output)))
};
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, specta::Type)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NodeIdentifier {
@ -72,16 +90,62 @@ impl PartialEq for TypeDescriptor {
pub enum Type {
Generic(Cow<'static, str>),
Concrete(TypeDescriptor),
Fn(Box<Type>, Box<Type>),
}
impl Type {
pub fn is_generic(&self) -> bool {
matches!(self, Type::Generic(_))
}
pub fn is_concrete(&self) -> bool {
matches!(self, Type::Concrete(_))
}
pub fn is_fn(&self) -> bool {
matches!(self, Type::Fn(_, _))
}
pub fn is_value(&self) -> bool {
matches!(self, Type::Fn(_, _) | Type::Concrete(_))
}
pub fn is_unit(&self) -> bool {
matches!(self, Type::Fn(_, _) | Type::Concrete(_))
}
pub fn is_generic_or_fn(&self) -> bool {
matches!(self, Type::Fn(_, _) | Type::Generic(_))
}
pub fn fn_input(&self) -> Option<&Type> {
match self {
Type::Fn(first, _) => Some(first),
_ => None,
}
}
pub fn fn_output(&self) -> Option<&Type> {
match self {
Type::Fn(_, second) => Some(second),
_ => None,
}
}
pub fn function(input: &Type, output: &Type) -> Type {
Type::Fn(Box::new(input.clone()), Box::new(output.clone()))
}
}
impl core::fmt::Debug for Type {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Generic(arg0) => f.write_fmt(format_args!("Generic({})", arg0)),
Self::Generic(arg0) => write!(f, "Generic({})", arg0),
#[cfg(feature = "type_id_logging")]
Self::Concrete(arg0) => f.write_fmt(format_args!("Concrete({}, {:?}))", arg0.name, arg0.id)),
Self::Concrete(arg0) => write!(f, "Concrete({}, {:?})", arg0.name, arg0.id),
#[cfg(not(feature = "type_id_logging"))]
Self::Concrete(arg0) => f.write_fmt(format_args!("Concrete({})", arg0.name)),
Self::Concrete(arg0) => write!(f, "Concrete({})", arg0.name),
Self::Fn(arg0, arg1) => write!(f, "({:?} -> {:?})", arg0, arg1),
}
}
}
@ -91,6 +155,7 @@ impl std::fmt::Display for Type {
match self {
Type::Generic(name) => write!(f, "{}", name),
Type::Concrete(ty) => write!(f, "{}", ty.name),
Type::Fn(input, output) => write!(f, "({} -> {})", input, output),
}
}
}

View file

@ -1,4 +1,5 @@
use core::marker::PhantomData;
use dyn_any::{DynAny, StaticType, StaticTypeSized};
use crate::Node;
@ -15,6 +16,10 @@ impl<'i, const N: u32> Node<'i, ()> for IntNode<N> {
#[derive(Default, Debug)]
pub struct ValueNode<T>(pub T);
impl<T: StaticTypeSized> StaticType for ValueNode<T> {
type Static = ValueNode<T::Static>;
}
impl<'i, T: 'i> Node<'i, ()> for ValueNode<T> {
type Output = &'i T;
fn eval<'s: 'i>(&'s self, _input: ()) -> Self::Output {
@ -43,6 +48,13 @@ impl<T: Clone + Copy> Copy for ValueNode<T> {}
#[derive(Clone)]
pub struct ClonedNode<T: Clone>(pub T);
impl<T: Clone + StaticTypeSized> StaticType for ClonedNode<T>
where
T::Static: Clone,
{
type Static = ClonedNode<T::Static>;
}
impl<'i, T: Clone + 'i> Node<'i, ()> for ClonedNode<T> {
type Output = T;
fn eval<'s: 'i>(&'s self, _input: ()) -> Self::Output {

View file

@ -80,9 +80,11 @@ checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bezier-rs"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"dyn-any",
"glam",
"serde",
]
[[package]]
@ -421,7 +423,6 @@ dependencies = [
"log",
"num-traits",
"nvtx",
"rand_chacha",
"serde",
"serde_json",
"spirv-builder",
@ -435,6 +436,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"base64",
"bezier-rs",
"bytemuck",
"dyn-any",
"dyn-clone",
@ -442,9 +444,9 @@ dependencies = [
"graphene-core",
"log",
"num-traits",
"rand_chacha",
"serde",
"specta",
"xxhash-rust",
]
[[package]]
@ -459,8 +461,10 @@ dependencies = [
"log",
"node-macro",
"once_cell",
"rand_chacha",
"serde",
"specta",
"spin",
]
[[package]]
@ -620,6 +624,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9275e0933cf8bb20f008924c0cb07a0692fe54d8064996520bf998de9eb79aa"
[[package]]
name = "lock_api"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.17"
@ -963,6 +977,12 @@ dependencies = [
"regex",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scratch"
version = "1.0.3"
@ -1061,6 +1081,15 @@ dependencies = [
"termcolor",
]
[[package]]
name = "spin"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5d6e0250b93c8427a177b849d144a96d5acc57006149479403d7861ab721e34"
dependencies = [
"lock_api",
]
[[package]]
name = "spirt"
version = "0.1.0"
@ -1404,3 +1433,9 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "xxhash-rust"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "735a71d46c4d68d71d4b24d03fdc2b98e38cea81730595801db779c04fe80d70"

View file

@ -47,7 +47,7 @@ impl DocumentNode {
.inputs
.iter()
.enumerate()
.filter(|(_, input)| matches!(input, NodeInput::Network(_)))
.filter(|(_, input)| matches!(input, NodeInput::Network(_) | NodeInput::ShortCircut(_)))
.nth(offset)
.unwrap_or_else(|| panic!("no network input found for {self:#?} and offset: {offset}"));
@ -72,6 +72,7 @@ impl DocumentNode {
NodeInput::ShortCircut(ty) => (ProtoNodeInput::ShortCircut(ty), ConstructionArgs::Nodes(vec![])),
};
assert!(!self.inputs.iter().any(|input| matches!(input, NodeInput::Network(_))), "recieved non resolved parameter");
assert!(!self.inputs.iter().any(|input| matches!(input, NodeInput::ShortCircut(_))), "recieved non resolved parameter");
assert!(
!self.inputs.iter().any(|input| matches!(input, NodeInput::Value { .. })),
"recieved value as parameter. inupts: {:#?}, construction_args: {:#?}",
@ -218,6 +219,12 @@ pub enum DocumentNodeImplementation {
Unresolved(NodeIdentifier),
}
impl Default for DocumentNodeImplementation {
fn default() -> Self {
Self::Unresolved(NodeIdentifier::new("graphene_cored::ops::IdNode"))
}
}
impl DocumentNodeImplementation {
pub fn get_network(&self) -> Option<&NodeNetwork> {
if let DocumentNodeImplementation::Network(n) = self {
@ -260,6 +267,227 @@ pub struct NodeNetwork {
pub previous_outputs: Option<Vec<NodeOutput>>,
}
/// Graph modification functions
impl NodeNetwork {
/// Get the original output nodes of this network, ignoring any preview node
pub fn original_outputs(&self) -> &Vec<NodeOutput> {
self.previous_outputs.as_ref().unwrap_or(&self.outputs)
}
pub fn input_types<'a>(&'a self) -> impl Iterator<Item = Type> + 'a {
self.inputs.iter().map(move |id| self.nodes[id].inputs.get(0).map(|i| i.ty().clone()).unwrap_or(concrete!(())))
}
/// An empty graph
pub fn value_network(node: DocumentNode) -> Self {
Self {
inputs: vec![0],
outputs: vec![NodeOutput::new(0, 0)],
nodes: [(0, node)].into_iter().collect(),
disabled: vec![],
previous_outputs: None,
}
}
/// A graph with just an input node
pub fn new_network() -> Self {
Self {
inputs: vec![0],
outputs: vec![NodeOutput::new(0, 0)],
nodes: [(
0,
DocumentNode {
name: "Input Frame".into(),
inputs: vec![NodeInput::ShortCircut(concrete!(u32))],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::ops::IdNode".into()),
metadata: DocumentNodeMetadata { position: (8, 4).into() },
},
)]
.into_iter()
.collect(),
..Default::default()
}
}
/// Appends a new node to the network after the output node and sets it as the new output
pub fn push_node(&mut self, mut node: DocumentNode) -> NodeId {
let id = self.nodes.len().try_into().expect("Too many nodes in network");
// Set the correct position for the new node
if let Some(pos) = self.nodes.get(&self.original_outputs()[0].node_id).map(|n| n.metadata.position) {
node.metadata.position = pos + IVec2::new(8, 0);
}
if self.outputs.is_empty() {
self.outputs.push(NodeOutput::new(id, 0));
}
let input = NodeInput::node(self.outputs[0].node_id, self.outputs[0].node_output_index);
if node.inputs.is_empty() {
node.inputs.push(input);
} else {
node.inputs[0] = input;
}
self.nodes.insert(id, node);
self.outputs = vec![NodeOutput::new(id, 0)];
id
}
/// Adds a output identity node to the network
pub fn push_output_node(&mut self) -> NodeId {
let node = DocumentNode {
name: "Output".into(),
inputs: vec![],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::ops::IdNode".into()),
metadata: DocumentNodeMetadata { position: (0, 0).into() },
};
self.push_node(node)
}
/// Adds a Cache and a Clone node to the network
pub fn push_cache_node(&mut self, ty: Type) -> NodeId {
let node = DocumentNode {
name: "Cache".into(),
inputs: vec![],
implementation: DocumentNodeImplementation::Network(NodeNetwork {
inputs: vec![0],
outputs: vec![NodeOutput::new(1, 0)],
nodes: vec![
(
0,
DocumentNode {
name: "CacheNode".to_string(),
inputs: vec![NodeInput::ShortCircut(concrete!(())), NodeInput::Network(ty)],
implementation: DocumentNodeImplementation::Unresolved(NodeIdentifier::new("graphene_std::memo::CacheNode")),
metadata: Default::default(),
},
),
(
1,
DocumentNode {
name: "CloneNode".to_string(),
inputs: vec![NodeInput::node(0, 0)],
implementation: DocumentNodeImplementation::Unresolved(NodeIdentifier::new("graphene_core::ops::CloneNode<_>")),
metadata: Default::default(),
},
),
]
.into_iter()
.collect(),
..Default::default()
}),
metadata: DocumentNodeMetadata { position: (0, 0).into() },
};
self.push_node(node)
}
/// Get the nested network given by the path of node ids
pub fn nested_network(&self, nested_path: &[NodeId]) -> Option<&Self> {
let mut network = Some(self);
for segment in nested_path {
network = network.and_then(|network| network.nodes.get(segment)).and_then(|node| node.implementation.get_network());
}
network
}
/// Get the mutable nested network given by the path of node ids
pub fn nested_network_mut(&mut self, nested_path: &[NodeId]) -> Option<&mut Self> {
let mut network = Some(self);
for segment in nested_path {
network = network.and_then(|network| network.nodes.get_mut(segment)).and_then(|node| node.implementation.get_network_mut());
}
network
}
/// Check if the specified node id is connected to the output
pub fn connected_to_output(&self, target_node_id: NodeId, ignore_imaginate: bool) -> bool {
// If the node is the output then return true
if self.outputs.iter().any(|&NodeOutput { node_id, .. }| node_id == target_node_id) {
return true;
}
// Get the outputs
let Some(mut stack) = self.outputs.iter().map(|&output| self.nodes.get(&output.node_id)).collect::<Option<Vec<_>>>() else {
return false;
};
let mut already_visited = HashSet::new();
already_visited.extend(self.outputs.iter().map(|output| output.node_id));
while let Some(node) = stack.pop() {
// Skip the imaginate node inputs
if ignore_imaginate && node.name == "Imaginate" {
continue;
}
for input in &node.inputs {
if let &NodeInput::Node { node_id: ref_id, .. } = input {
// Skip if already viewed
if already_visited.contains(&ref_id) {
continue;
}
// If the target node is used as input then return true
if ref_id == target_node_id {
return true;
}
// Add the referenced node to the stack
let Some(ref_node) = self.nodes.get(&ref_id) else {
continue;
};
already_visited.insert(ref_id);
stack.push(ref_node);
}
}
}
false
}
/// Is the node being used directly as an output?
pub fn outputs_contain(&self, node_id: NodeId) -> bool {
self.outputs.iter().any(|output| output.node_id == node_id)
}
/// Is the node being used directly as an original output?
pub fn original_outputs_contain(&self, node_id: NodeId) -> bool {
self.original_outputs().iter().any(|output| output.node_id == node_id)
}
/// Is the node being used directly as a previous output?
pub fn previous_outputs_contain(&self, node_id: NodeId) -> Option<bool> {
self.previous_outputs.as_ref().map(|outputs| outputs.iter().any(|output| output.node_id == node_id))
}
/// A iterator of all nodes connected by primary inputs.
///
/// Used for the properties panel and tools.
pub fn primary_flow(&self) -> impl Iterator<Item = (&DocumentNode, u64)> {
struct FlowIter<'a> {
stack: Vec<NodeId>,
network: &'a NodeNetwork,
}
impl<'a> Iterator for FlowIter<'a> {
type Item = (&'a DocumentNode, NodeId);
fn next(&mut self) -> Option<Self::Item> {
loop {
let node_id = self.stack.pop()?;
if let Some(document_node) = self.network.nodes.get(&node_id) {
self.stack.extend(
document_node
.inputs
.iter()
.take(1) // Only show the primary input
.filter_map(|input| if let NodeInput::Node { node_id: ref_id, .. } = input { Some(*ref_id) } else { None }),
);
return Some((document_node, node_id));
};
}
}
}
FlowIter {
stack: self.outputs.iter().map(|output| output.node_id).collect(),
network: &self,
}
}
}
/// Functions for compiling the network
impl NodeNetwork {
pub fn map_ids(&mut self, f: impl Fn(NodeId) -> NodeId + Copy) {
self.inputs.iter_mut().for_each(|id| *id = f(*id));
@ -405,6 +633,31 @@ impl NodeNetwork {
self.nodes.insert(id, node);
return;
}
// replace value inputs with value nodes
for input in &mut node.inputs {
let mut dummy_input = NodeInput::ShortCircut(concrete!(()));
std::mem::swap(&mut dummy_input, input);
if let NodeInput::Value { tagged_value, exposed } = dummy_input {
let value_node_id = gen_id();
let value_node_id = map_ids(id, value_node_id);
self.nodes.insert(
value_node_id,
DocumentNode {
name: "Value".into(),
inputs: vec![NodeInput::Value { tagged_value, exposed }],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::value::ValueNode".into()),
metadata: DocumentNodeMetadata::default(),
},
);
*input = NodeInput::Node {
node_id: value_node_id,
output_index: 0,
lambda: false,
};
} else {
*input = dummy_input;
}
}
match node.implementation {
DocumentNodeImplementation::Network(mut inner_network) => {
@ -423,20 +676,6 @@ impl NodeNetwork {
let network_input = self.nodes.get_mut(network_input).unwrap();
network_input.populate_first_network_input(node_id, output_index, *offset, lambda);
}
NodeInput::Value { tagged_value, exposed } => {
let name = "Value".to_string();
let new_id = map_ids(id, gen_id());
let value_node = DocumentNode {
name,
inputs: vec![NodeInput::Value { tagged_value, exposed }],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::value::ValueNode".into()),
metadata: DocumentNodeMetadata::default(),
};
assert!(!self.nodes.contains_key(&new_id));
self.nodes.insert(new_id, value_node);
let network_input = self.nodes.get_mut(network_input).unwrap();
network_input.populate_first_network_input(new_id, 0, *offset, false);
}
NodeInput::Network(_) => {
*network_offsets.get_mut(network_input).unwrap() += 1;
if let Some(index) = self.inputs.iter().position(|i| *i == id) {
@ -444,6 +683,7 @@ impl NodeNetwork {
}
}
NodeInput::ShortCircut(_) => (),
NodeInput::Value { .. } => unreachable!("Value inputs should have been replaced with value nodes"),
}
}
node.implementation = DocumentNodeImplementation::Unresolved("graphene_core::ops::IdNode".into());
@ -461,7 +701,7 @@ impl NodeNetwork {
self.flatten_with_fns(node_id, map_ids, gen_id);
}
}
DocumentNodeImplementation::Unresolved(_) => (),
DocumentNodeImplementation::Unresolved(_) => {}
}
assert!(!self.nodes.contains_key(&id), "Trying to insert a node into the network caused an id conflict");
self.nodes.insert(id, node);
@ -478,151 +718,6 @@ impl NodeNetwork {
nodes: nodes.clone(),
})
}
/// Get the original output nodes of this network, ignoring any preview node
pub fn original_outputs(&self) -> &Vec<NodeOutput> {
self.previous_outputs.as_ref().unwrap_or(&self.outputs)
}
/// A graph with just an input and output node
pub fn new_network(output_offset: i32, output_node_id: NodeId) -> Self {
Self {
inputs: vec![0],
outputs: vec![NodeOutput::new(1, 0)],
nodes: [
(
0,
DocumentNode {
name: "Input Frame".into(),
inputs: vec![NodeInput::Network(concrete!(u32))],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::ops::IdNode".into()),
metadata: DocumentNodeMetadata { position: (8, 4).into() },
},
),
(
1,
DocumentNode {
name: "Output".into(),
inputs: vec![NodeInput::node(output_node_id, 0)],
implementation: DocumentNodeImplementation::Unresolved("graphene_core::ops::IdNode".into()),
metadata: DocumentNodeMetadata { position: (output_offset, 4).into() },
},
),
]
.into_iter()
.collect(),
..Default::default()
}
}
/// Get the nested network given by the path of node ids
pub fn nested_network(&self, nested_path: &[NodeId]) -> Option<&Self> {
let mut network = Some(self);
for segment in nested_path {
network = network.and_then(|network| network.nodes.get(segment)).and_then(|node| node.implementation.get_network());
}
network
}
/// Get the mutable nested network given by the path of node ids
pub fn nested_network_mut(&mut self, nested_path: &[NodeId]) -> Option<&mut Self> {
let mut network = Some(self);
for segment in nested_path {
network = network.and_then(|network| network.nodes.get_mut(segment)).and_then(|node| node.implementation.get_network_mut());
}
network
}
/// Check if the specified node id is connected to the output
pub fn connected_to_output(&self, target_node_id: NodeId, ignore_imaginate: bool) -> bool {
// If the node is the output then return true
if self.outputs.iter().any(|&NodeOutput { node_id, .. }| node_id == target_node_id) {
return true;
}
// Get the outputs
let Some(mut stack) = self.outputs.iter().map(|&output| self.nodes.get(&output.node_id)).collect::<Option<Vec<_>>>() else {
return false;
};
let mut already_visited = HashSet::new();
already_visited.extend(self.outputs.iter().map(|output| output.node_id));
while let Some(node) = stack.pop() {
// Skip the imaginate node inputs
if ignore_imaginate && node.name == "Imaginate" {
continue;
}
for input in &node.inputs {
if let &NodeInput::Node { node_id: ref_id, .. } = input {
// Skip if already viewed
if already_visited.contains(&ref_id) {
continue;
}
// If the target node is used as input then return true
if ref_id == target_node_id {
return true;
}
// Add the referenced node to the stack
let Some(ref_node) = self.nodes.get(&ref_id) else {
continue;
};
already_visited.insert(ref_id);
stack.push(ref_node);
}
}
}
false
}
/// Is the node being used directly as an output?
pub fn outputs_contain(&self, node_id: NodeId) -> bool {
self.outputs.iter().any(|output| output.node_id == node_id)
}
/// Is the node being used directly as an original output?
pub fn original_outputs_contain(&self, node_id: NodeId) -> bool {
self.original_outputs().iter().any(|output| output.node_id == node_id)
}
/// Is the node being used directly as a previous output?
pub fn previous_outputs_contain(&self, node_id: NodeId) -> Option<bool> {
self.previous_outputs.as_ref().map(|outputs| outputs.iter().any(|output| output.node_id == node_id))
}
/// A iterator of all nodes connected by primary inputs.
///
/// Used for the properties panel and tools.
pub fn primary_flow(&self) -> impl Iterator<Item = (&DocumentNode, u64)> {
struct FlowIter<'a> {
stack: Vec<NodeId>,
network: &'a NodeNetwork,
}
impl<'a> Iterator for FlowIter<'a> {
type Item = (&'a DocumentNode, NodeId);
fn next(&mut self) -> Option<Self::Item> {
loop {
let node_id = self.stack.pop()?;
if let Some(document_node) = self.network.nodes.get(&node_id) {
self.stack.extend(
document_node
.inputs
.iter()
.take(1) // Only show the primary input
.filter_map(|input| if let NodeInput::Node { node_id: ref_id, .. } = input { Some(*ref_id) } else { None }),
);
return Some((document_node, node_id));
};
}
}
}
FlowIter {
stack: self.outputs.iter().map(|output| output.node_id).collect(),
network: &self,
}
}
}
#[cfg(test)]

View file

@ -47,6 +47,7 @@ pub enum TaggedValue {
Quantization(graphene_core::quantization::QuantizationChannels),
OptionalColor(Option<graphene_core::raster::color::Color>),
ManipulatorGroupIds(Vec<graphene_core::uuid::ManipulatorGroupId>),
VecDVec2(Vec<DVec2>),
}
#[allow(clippy::derived_hash_with_manual_eq)]
@ -104,6 +105,12 @@ impl Hash for TaggedValue {
Self::Quantization(quantized_image) => quantized_image.hash(state),
Self::OptionalColor(color) => color.hash(state),
Self::ManipulatorGroupIds(mirror) => mirror.hash(state),
Self::VecDVec2(vec_dvec2) => {
vec_dvec2.len().hash(state);
for dvec2 in vec_dvec2 {
dvec2.to_array().iter().for_each(|x| x.to_bits().hash(state));
}
}
}
}
}
@ -145,6 +152,7 @@ impl<'a> TaggedValue {
TaggedValue::Quantization(x) => Box::new(x),
TaggedValue::OptionalColor(x) => Box::new(x),
TaggedValue::ManipulatorGroupIds(x) => Box::new(x),
TaggedValue::VecDVec2(x) => Box::new(x),
}
}
@ -185,6 +193,7 @@ impl<'a> TaggedValue {
TaggedValue::Quantization(_) => concrete!(graphene_core::quantization::QuantizationChannels),
TaggedValue::OptionalColor(_) => concrete!(Option<graphene_core::Color>),
TaggedValue::ManipulatorGroupIds(_) => concrete!(Vec<graphene_core::uuid::ManipulatorGroupId>),
TaggedValue::VecDVec2(_) => concrete!(Vec<DVec2>),
}
}
}

View file

@ -403,6 +403,11 @@ impl TypingContext {
self.constructor.get(&node_id).copied()
}
/// Returns the type of a given node id if it exists
pub fn type_of(&self, node_id: NodeId) -> Option<&NodeIOTypes> {
self.inferred.get(&node_id)
}
/// Returns the inferred types for a given node id.
pub fn infer(&mut self, node_id: NodeId, node: &ProtoNode) -> Result<NodeIOTypes, String> {
let identifier = node.identifier.name.clone();
@ -416,7 +421,8 @@ impl TypingContext {
// If the node has a value parameter we can infer the return type from it
ConstructionArgs::Value(ref v) => {
assert!(matches!(node.input, ProtoNodeInput::None));
let types = NodeIOTypes::new(concrete!(()), v.ty(), vec![]);
// TODO: This should return a reference to the value
let types = NodeIOTypes::new(concrete!(()), v.ty(), vec![v.ty()]);
self.inferred.insert(node_id, types.clone());
return Ok(types);
}
@ -427,9 +433,9 @@ impl TypingContext {
self.inferred
.get(id)
.ok_or(format!("Inferring type of {node_id} depends on {id} which is not present in the typing context"))
.map(|node| (node.input.clone(), node.output.clone()))
.map(|node| node.ty())
})
.collect::<Result<Vec<(Type, Type)>, String>>()?,
.collect::<Result<Vec<Type>, String>>()?,
};
// Get the node input type from the proto node declaration
@ -450,26 +456,27 @@ impl TypingContext {
if matches!(input, Type::Generic(_)) {
return Err(format!("Generic types are not supported as inputs yet {:?} occured in {:?}", &input, node.identifier));
}
if parameters.iter().any(|p| matches!(p.1, Type::Generic(_))) {
if parameters.iter().any(|p| match p {
Type::Fn(_, b) if matches!(b.as_ref(), Type::Generic(_)) => true,
_ => false,
}) {
return Err(format!("Generic types are not supported in parameters: {:?} occured in {:?}", parameters, node.identifier));
}
let covariant = |output, input| match (&output, &input) {
(Type::Concrete(t1), Type::Concrete(t2)) => t1 == t2,
(Type::Concrete(_), Type::Generic(_)) => true,
// TODO: relax this requirement when allowing generic types as inputs
(Type::Generic(_), _) => false,
};
fn covariant(from: &Type, to: &Type) -> bool {
match (from, to) {
(Type::Concrete(t1), Type::Concrete(t2)) => t1 == t2,
(Type::Fn(a1, b1), Type::Fn(a2, b2)) => covariant(a1, a2) && covariant(b1, b2),
// TODO: relax this requirement when allowing generic types as inputs
(Type::Generic(_), _) => false,
(_, Type::Generic(_)) => true,
_ => false,
}
}
// List of all implementations that match the input and parameter types
let valid_output_types = impls
.keys()
.filter(|node_io| {
covariant(input.clone(), node_io.input.clone())
&& parameters
.iter()
.zip(node_io.parameters.iter())
.all(|(p1, p2)| covariant(p1.0.clone(), p2.0.clone()) && covariant(p1.1.clone(), p2.1.clone()))
})
.filter(|node_io| covariant(&input, &node_io.input) && parameters.iter().zip(node_io.parameters.iter()).all(|(p1, p2)| covariant(p1, p2) && covariant(p1, p2)))
.collect::<Vec<_>>();
// Attempt to substitute generic types with concrete types and save the list of results
@ -517,7 +524,7 @@ impl TypingContext {
/// Returns a list of all generic types used in the node
fn collect_generics(types: &NodeIOTypes) -> Vec<Cow<'static, str>> {
let inputs = [&types.input].into_iter().chain(types.parameters.iter().map(|(_, x)| x));
let inputs = [&types.input].into_iter().chain(types.parameters.iter().flat_map(|x| x.fn_output()));
let mut generics = inputs
.filter_map(|t| match t {
Type::Generic(out) => Some(out.clone()),
@ -532,15 +539,16 @@ fn collect_generics(types: &NodeIOTypes) -> Vec<Cow<'static, str>> {
}
/// Checks if a generic type can be substituted with a concrete type and returns the concrete type
fn check_generic(types: &NodeIOTypes, input: &Type, parameters: &[(Type, Type)], generic: &str) -> Result<Type, String> {
let inputs = [(&types.input, input)]
fn check_generic(types: &NodeIOTypes, input: &Type, parameters: &[Type], generic: &str) -> Result<Type, String> {
let inputs = [(Some(&types.input), Some(input))]
.into_iter()
.chain(types.parameters.iter().map(|(_, x)| x).zip(parameters.iter().map(|(_, x)| x)));
let mut concrete_inputs = inputs.filter(|(ni, _)| matches!(ni, Type::Generic(input) if generic == input));
let (_, out_ty) = concrete_inputs
.chain(types.parameters.iter().map(|x| x.fn_output()).zip(parameters.iter().map(|x| x.fn_output())));
let concrete_inputs = inputs.filter(|(ni, _)| matches!(ni, Some(Type::Generic(input)) if generic == input));
let mut outputs = concrete_inputs.flat_map(|(_, out)| out);
let out_ty = outputs
.next()
.ok_or_else(|| format!("Generic output type {generic} is not dependent on input {input:?} or parameters {parameters:?}",))?;
if concrete_inputs.any(|(_, ty)| ty != out_ty) {
if outputs.any(|ty| ty != out_ty) {
return Err(format!("Generic output type {generic} is dependent on multiple inputs or parameters",));
}
Ok(out_ty.clone())

View file

@ -93,7 +93,7 @@ impl<N: Clone, O: StaticType> Clone for DowncastNode<O, N> {
impl<N: Copy, O: StaticType> Copy for DowncastNode<O, N> {}
#[node_macro::node_fn(DowncastNode<_O>)]
fn downcast<N, _O: StaticType>(input: Any<'input>, node: &'input N) -> _O
fn downcast<N: 'input, _O: StaticType>(input: Any<'input>, node: &'input N) -> _O
where
N: for<'any_input> Node<'any_input, Any<'any_input>, Output = Any<'any_input>> + 'input,
{

View file

@ -0,0 +1,215 @@
use std::marker::PhantomData;
use glam::{DAffine2, DVec2};
use graphene_core::raster::{Color, Image, ImageFrame};
use graphene_core::transform::TransformMut;
use graphene_core::vector::VectorData;
use graphene_core::Node;
use node_macro::node_fn;
// Spacing is a consistent 0.2 apart, even when tiled across pixels (from 0.9 to the neighboring 0.1), to avoid bias
const MULTISAMPLE_GRID: [(f64, f64); 25] = [
// Row 1
(0.1, 0.1),
(0.1, 0.3),
(0.1, 0.5),
(0.1, 0.7),
(0.1, 0.9),
// Row 2
(0.3, 0.1),
(0.3, 0.3),
(0.3, 0.5),
(0.3, 0.7),
(0.3, 0.9),
// Row 3
(0.5, 0.1),
(0.5, 0.3),
(0.5, 0.5),
(0.5, 0.7),
(0.5, 0.9),
// Row 4
(0.7, 0.1),
(0.7, 0.3),
(0.7, 0.5),
(0.7, 0.7),
(0.7, 0.9),
// Row 5
(0.9, 0.1),
(0.9, 0.3),
(0.9, 0.5),
(0.9, 0.7),
(0.9, 0.9),
];
#[derive(Clone, Debug, PartialEq)]
pub struct ReduceNode<Initial, Lambda> {
pub initial: Initial,
pub lambda: Lambda,
}
#[node_fn(ReduceNode)]
fn reduce<I: Iterator, Lambda, T>(iter: I, initial: T, lambda: &'any_input Lambda) -> T
where
Lambda: for<'a> Node<'a, (T, I::Item), Output = T>,
{
iter.fold(initial, |a, x| lambda.eval((a, x)))
}
#[derive(Clone, Debug, PartialEq)]
pub struct IntoIterNode<T> {
_t: PhantomData<T>,
}
#[node_fn(IntoIterNode<_T>)]
fn into_iter<'i: 'input, _T: Send + Sync>(vec: &'i Vec<_T>) -> Box<dyn Iterator<Item = &'i _T> + Send + Sync + 'i> {
Box::new(vec.iter())
}
#[derive(Clone, Debug, PartialEq)]
pub struct VectorPointsNode;
#[node_fn(VectorPointsNode)]
fn vector_points(vector: VectorData) -> Vec<DVec2> {
vector.subpaths.iter().flat_map(|subpath| subpath.manipulator_groups().iter().map(|group| group.anchor)).collect()
}
#[derive(Clone, Debug, PartialEq)]
pub struct BrushTextureNode<ColorNode, Hardness, Flow> {
pub color: ColorNode,
pub hardness: Hardness,
pub flow: Flow,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EraseNode<Flow> {
flow: Flow,
}
#[node_fn(EraseNode)]
fn erase(input: (Color, Color), flow: f64) -> Color {
let (input, brush) = input;
let alpha = input.a() * (1.0 - flow as f32 * brush.a());
Color::from_unassociated_alpha(input.r(), input.g(), input.b(), alpha)
}
#[node_fn(BrushTextureNode)]
fn brush_texture(diameter: f64, color: Color, hardness: f64, flow: f64) -> ImageFrame {
// Diameter
let radius = diameter / 2.;
// TODO: Remove the 4px padding after figuring out why the brush stamp gets randomly offset by 1px up/down/left/right when clicking with the Brush tool
let dimension = diameter.ceil() as u32 + 4;
let center = DVec2::splat(radius + (dimension as f64 - diameter) / 2.);
// Hardness
let hardness = hardness / 100.;
let feather_exponent = 1. / (1. - hardness);
// Flow
let flow = flow / 100.;
// Color
let color = color.apply_opacity(flow as f32);
// Initial transparent image
let mut image = Image::new(dimension, dimension, Color::TRANSPARENT);
for y in 0..dimension {
for x in 0..dimension {
let summation = MULTISAMPLE_GRID.iter().fold(0., |acc, (offset_x, offset_y)| {
let position = DVec2::new(x as f64 + offset_x, y as f64 + offset_y);
let distance = (position - center).length();
if distance < radius {
acc + (1. - (distance / radius).powf(feather_exponent)).clamp(0., 1.)
} else {
acc
}
});
let pixel_fill = summation / MULTISAMPLE_GRID.len() as f64;
let pixel = image.get_mut(x, y).unwrap();
*pixel = color.apply_opacity(pixel_fill as f32);
}
}
ImageFrame {
image,
transform: DAffine2::from_scale_angle_translation(DVec2::splat(dimension as f64), 0., -DVec2::splat(radius)),
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TranslateNode<Translatable> {
translatable: Translatable,
}
#[node_fn(TranslateNode)]
fn translate_node<Data: TransformMut>(offset: DVec2, mut translatable: Data) -> Data {
translatable.translate(offset);
translatable
}
#[cfg(test)]
mod test {
use super::*;
use crate::raster::*;
use glam::DAffine2;
use graphene_core::ops::{AddNode, CloneNode};
use graphene_core::raster::*;
use graphene_core::structural::Then;
use graphene_core::transform::{Transform, TransformMut};
use graphene_core::value::{ClonedNode, ValueNode};
#[test]
fn test_translate_node() {
let image = Image::new(10, 10, Color::TRANSPARENT);
let mut image = ImageFrame { image, transform: DAffine2::IDENTITY };
image.translate(DVec2::new(1.0, 2.0));
let translate_node = TranslateNode::new(ClonedNode::new(image));
let image = translate_node.eval(DVec2::new(1.0, 2.0));
assert_eq!(image.transform(), DAffine2::from_translation(DVec2::new(2.0, 4.0)));
}
#[test]
fn test_reduce() {
let reduce_node = ReduceNode::new(ClonedNode::new(0u32), ValueNode::new(AddNode));
let sum = reduce_node.eval(vec![1, 2, 3, 4, 5].into_iter());
assert_eq!(sum, 15);
}
#[test]
fn test_brush_texture() {
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(100.), ClonedNode::new(100.));
let size = 20.;
let image = brush_texture_node.eval(size);
assert_eq!(image.image.width, size.ceil() as u32 + 4);
assert_eq!(image.image.height, size.ceil() as u32 + 4);
assert_eq!(image.transform, DAffine2::from_scale_angle_translation(DVec2::splat(size.ceil() + 4.), 0., -DVec2::splat(size / 2.)));
// center pixel should be BLACK
assert_eq!(image.image.get(11, 11), Some(&Color::BLACK));
}
#[test]
fn test_brush() {
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(1.0), ClonedNode::new(1.0));
let image = brush_texture_node.eval(20.);
let trace = vec![DVec2::new(0.0, 0.0), DVec2::new(10.0, 0.0)];
let trace = ClonedNode::new(trace.into_iter());
let translate_node = TranslateNode::new(ClonedNode::new(image));
let frames = MapNode::new(ValueNode::new(translate_node));
let frames = trace.then(frames).eval(()).collect::<Vec<_>>();
assert_eq!(frames.len(), 2);
assert_eq!(frames[0].image.width, 24);
let background_bounds = ReduceNode::new(ClonedNode::new(None), ValueNode::new(MergeBoundingBoxNode::new()));
let background_bounds = background_bounds.eval(frames.clone().into_iter());
let background_bounds = ClonedNode::new(background_bounds.unwrap().to_transform());
let background_image = background_bounds.then(EmptyImageNode::new(ClonedNode::new(Color::TRANSPARENT)));
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(BlendMode::Normal), ClonedNode::new(1.0));
let final_image = ReduceNode::new(background_image, ValueNode::new(BlendImageTupleNode::new(ValueNode::new(blend_node))));
let final_image = final_image.eval(frames.into_iter());
assert_eq!(final_image.image.height, 24);
assert_eq!(final_image.image.width, 34);
drop(final_image);
}
}

View file

@ -19,3 +19,5 @@ pub mod executor;
pub mod quantization;
pub use graphene_core::*;
pub mod brush;

View file

@ -1,9 +1,12 @@
use dyn_any::{DynAny, StaticType};
use dyn_any::{DynAny, StaticType, StaticTypeSized};
use glam::{BVec2, DAffine2, DVec2};
use graphene_core::raster::{Color, Image, ImageFrame};
use graphene_core::transform::Transform;
use graphene_core::value::{ClonedNode, ValueNode};
use graphene_core::Node;
use std::marker::PhantomData;
use std::path::Path;
#[derive(Debug, DynAny)]
@ -139,6 +142,10 @@ pub struct MapImageFrameNode<MapFn> {
map_fn: MapFn,
}
impl<MapFn: dyn_any::StaticTypeSized> StaticType for MapImageFrameNode<MapFn> {
type Static = MapImageFrameNode<MapFn::Static>;
}
#[node_macro::node_fn(MapImageFrameNode)]
fn map_image<MapFn>(mut image_frame: ImageFrame, map_fn: &'any_input MapFn) -> ImageFrame
where
@ -151,12 +158,37 @@ where
image_frame
}
#[derive(Debug, Clone)]
struct AxisAlignedBbox {
#[derive(Debug, Clone, DynAny)]
pub struct AxisAlignedBbox {
start: DVec2,
end: DVec2,
}
impl AxisAlignedBbox {
pub fn size(&self) -> DVec2 {
self.end - self.start
}
pub fn to_transform(&self) -> DAffine2 {
DAffine2::from_translation(self.start) * DAffine2::from_scale(self.size())
}
pub fn contains(&self, point: DVec2) -> bool {
point.x >= self.start.x && point.x <= self.end.x && point.y >= self.start.y && point.y <= self.end.y
}
pub fn intersects(&self, other: &AxisAlignedBbox) -> bool {
other.start.x <= self.end.x && other.end.x >= self.start.x && other.start.y <= self.end.y && other.end.y >= self.start.y
}
pub fn union(&self, other: &AxisAlignedBbox) -> AxisAlignedBbox {
AxisAlignedBbox {
start: DVec2::new(self.start.x.min(other.start.x), self.start.y.min(other.start.y)),
end: DVec2::new(self.end.x.max(other.end.x), self.end.y.max(other.end.y)),
}
}
}
#[derive(Debug, Clone)]
struct Bbox {
top_left: DVec2,
@ -228,18 +260,42 @@ fn mask_image(mut image: ImageFrame, mask: ImageFrame) -> ImageFrame {
image
}
#[derive(Debug, Clone, Copy)]
pub struct BlendImageTupleNode<MapFn> {
map_fn: MapFn,
}
impl<MapFn: StaticTypeSized> StaticType for BlendImageTupleNode<MapFn> {
type Static = BlendImageTupleNode<MapFn::Static>;
}
#[node_macro::node_fn(BlendImageTupleNode)]
fn blend_image_tuple<MapFn>(images: (ImageFrame, ImageFrame), map_fn: &'any_input MapFn) -> ImageFrame
where
MapFn: for<'any_input> Node<'any_input, (Color, Color), Output = Color> + 'input + Clone,
{
let (mut background, foreground) = images;
let node = BlendImageNode::new(ClonedNode::new(background), ValueNode::new(map_fn.clone()));
node.eval(foreground)
}
#[derive(Debug, Clone, Copy)]
pub struct BlendImageNode<Background, MapFn> {
background: Background,
map_fn: MapFn,
}
impl<Background: StaticTypeSized, MapFn: StaticTypeSized> StaticType for BlendImageNode<Background, MapFn> {
type Static = BlendImageNode<Background::Static, MapFn::Static>;
}
// TODO: Implement proper blending
#[node_macro::node_fn(BlendImageNode)]
fn blend_image<MapFn>(foreground: ImageFrame, mut background: ImageFrame, map_fn: &'any_input MapFn) -> ImageFrame
fn blend_image<MapFn, Frame: AsRef<ImageFrame>>(foreground: Frame, mut background: ImageFrame, map_fn: &'any_input MapFn) -> ImageFrame
where
MapFn: for<'any_input> Node<'any_input, (Color, Color), Output = Color> + 'input,
{
let foreground = foreground.as_ref();
let foreground_size = DVec2::new(foreground.image.width as f64, foreground.image.height as f64);
let background_size = DVec2::new(background.image.width as f64, background.image.height as f64);
@ -271,6 +327,38 @@ where
background
}
#[derive(Clone, Debug, PartialEq)]
pub struct MergeBoundingBoxNode<Data> {
_data: PhantomData<Data>,
}
#[node_macro::node_fn(MergeBoundingBoxNode<_Data>)]
fn merge_bounding_box_node<_Data: Transform>(input: (Option<AxisAlignedBbox>, _Data)) -> Option<AxisAlignedBbox> {
let (initial_aabb, data) = input;
let snd_aabb = compute_transformed_bounding_box(data.transform()).axis_aligned_bbox();
if let Some(fst_aabb) = initial_aabb {
Some(fst_aabb.union(&snd_aabb))
} else {
Some(snd_aabb)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct EmptyImageNode<FillColor> {
pub color: FillColor,
}
#[node_macro::node_fn(EmptyImageNode)]
fn empty_image(transform: DAffine2, color: Color) -> ImageFrame {
let width = transform.transform_vector2(DVec2::new(1., 0.)).length() as u32;
let height = transform.transform_vector2(DVec2::new(0., 1.)).length() as u32;
let image = Image::new(width, height, color);
ImageFrame { image, transform }
}
#[derive(Debug, Clone, Copy)]
pub struct ImaginateNode<E> {
cached: E,

View file

@ -7,6 +7,7 @@ use graph_craft::document::value::UpcastNode;
use graph_craft::document::NodeId;
use graph_craft::executor::Executor;
use graph_craft::proto::{ConstructionArgs, ProtoNetwork, ProtoNode, TypingContext};
use graph_craft::{Type, TypeDescriptor};
use graphene_std::any::{Any, TypeErasedPinned, TypeErasedPinnedRef};
use crate::node_registry;
@ -48,6 +49,14 @@ impl DynamicExecutor {
}
Ok(())
}
pub fn input_type(&self) -> Option<Type> {
self.typing_context.type_of(self.output).map(|node_io| node_io.input.clone())
}
pub fn output_type(&self) -> Option<Type> {
self.typing_context.type_of(self.output).map(|node_io| node_io.output.clone())
}
}
impl Executor for DynamicExecutor {

View file

@ -10,6 +10,8 @@ use graphene_core::raster::*;
use graphene_core::structural::Then;
use graphene_core::value::{ClonedNode, ForgetNode, ValueNode};
use graphene_core::{Node, NodeIO, NodeIOTypes};
use graphene_std::brush::*;
use graphene_std::raster::*;
use graphene_std::any::{ComposeTypeErased, DowncastBothNode, DowncastBothRefNode, DynAnyInRefNode, DynAnyNode, DynAnyRefNode, IntoTypeErasedNode, TypeErasedPinnedRef};
@ -17,8 +19,9 @@ use graphene_core::{Cow, NodeIdentifier, Type, TypeDescriptor};
use graph_craft::proto::NodeConstructor;
use graphene_core::{concrete, generic};
use graphene_core::{concrete, fn_type, generic, value_fn};
use graphene_std::memo::{CacheNode, LetNode};
use graphene_std::raster::{BlendImageTupleNode, MapImageFrameNode};
use crate::executor::NodeContainer;
@ -55,7 +58,7 @@ macro_rules! register_node {
let node = <$path>::new($(
graphene_std::any::input_node::<$type>(_node)
),*);
let params = vec![$((concrete!(()), concrete!($type))),*];
let params = vec![$(value_fn!($type)),*];
let mut node_io = <$path as NodeIO<'_, $input>>::to_node_io(&node, params);
node_io.input = concrete!(<$input as StaticType>::Static);
node_io
@ -75,7 +78,7 @@ macro_rules! raster_node {
Box::pin(any)
},
{
let params = vec![$((concrete!(()), concrete!($type))),*];
let params = vec![$(value_fn!($type)),*];
NodeIOTypes::new(concrete!(Color), concrete!(Color), params)
},
),
@ -88,7 +91,7 @@ macro_rules! raster_node {
Box::pin(any)
},
{
let params = vec![$((concrete!(()), concrete!($type))),*];
let params = vec![$(value_fn!($type)),*];
NodeIOTypes::new(concrete!(Image), concrete!(Image), params)
},
),
@ -101,7 +104,7 @@ macro_rules! raster_node {
Box::pin(any)
},
{
let params = vec![$((concrete!(()), concrete!($type))),*];
let params = vec![$(value_fn!($type)),*];
NodeIOTypes::new(concrete!(ImageFrame), concrete!(ImageFrame), params)
},
)
@ -137,6 +140,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
register_node!(graphene_core::ops::SomeNode, input: ImageFrame, params: []),
register_node!(graphene_std::raster::DownresNode, input: ImageFrame, params: []),
register_node!(graphene_std::raster::MaskImageNode<_>, input: ImageFrame, params: [ImageFrame]),
register_node!(graphene_std::raster::EmptyImageNode<_>, input: DAffine2, params: [Color]),
#[cfg(feature = "gpu")]
register_node!(graphene_std::executor::MapGpuSingleImageNode<_>, input: Image, params: [String]),
vec![(
@ -145,30 +149,100 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let node = ComposeTypeErased::new(args[0], args[1]);
node.into_type_erased()
},
NodeIOTypes::new(generic!(T), generic!(U), vec![(generic!(T), generic!(V)), (generic!(V), generic!(U))]),
NodeIOTypes::new(
generic!(T),
generic!(U),
vec![Type::Fn(Box::new(generic!(T)), Box::new(generic!(V))), Type::Fn(Box::new(generic!(V)), Box::new(generic!(U)))],
),
)],
//register_node!(graphene_std::brush::ReduceNode<_, _>, input: core::slice::Iter<ImageFrame>, params: [ImageFrame, &ValueNode<BlendImageTupleNode<ValueNode<BlendNode<ClonedNode<BlendMode>, ClonedNode<f64>>>>>]),
//register_node!(graphene_std::brush::ReduceNode<_, _>, input: core::slice::Iter<ImageFrame>, params: [AxisAlignedBbox, &MergeBoundingBoxNode]),
register_node!(graphene_std::brush::IntoIterNode<_>, input: &Vec<DVec2>, params: []),
vec![(
NodeIdentifier::new("graphene_std::brush::BrushNode"),
|args| {
use graphene_std::brush::*;
let trace: DowncastBothNode<(), Vec<DVec2>> = DowncastBothNode::new(args[0]);
let diameter: DowncastBothNode<(), f64> = DowncastBothNode::new(args[1]);
let hardness: DowncastBothNode<(), f64> = DowncastBothNode::new(args[2]);
let flow: DowncastBothNode<(), f64> = DowncastBothNode::new(args[3]);
let color: DowncastBothNode<(), Color> = DowncastBothNode::new(args[4]);
let stamp = BrushTextureNode::new(color, ClonedNode::new(hardness.eval(())), ClonedNode::new(flow.eval(())));
let stamp = stamp.eval(diameter.eval(()));
let frames = TranslateNode::new(ClonedNode::new(stamp));
let frames = MapNode::new(ValueNode::new(frames));
let frames = frames.eval(trace.eval(()).into_iter()).collect::<Vec<_>>();
let background_bounds = ReduceNode::new(ClonedNode::new(None), ValueNode::new(MergeBoundingBoxNode::new()));
let background_bounds = background_bounds.eval(frames.clone().into_iter());
let background_bounds = ClonedNode::new(background_bounds.unwrap().to_transform());
let background_image = background_bounds.then(EmptyImageNode::new(ClonedNode::new(Color::TRANSPARENT)));
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(BlendMode::Normal), ClonedNode::new(100.));
let final_image = ReduceNode::new(background_image, ValueNode::new(BlendImageTupleNode::new(ValueNode::new(blend_node))));
let final_image = final_image.eval(frames.into_iter());
let final_image = ClonedNode::new(final_image);
let any: DynAnyNode<(), _, _> = graphene_std::any::DynAnyNode::new(ValueNode::new(final_image));
Box::pin(any)
},
NodeIOTypes::new(
concrete!(()),
concrete!(ImageFrame),
vec![value_fn!(Vec<DVec2>), value_fn!(f64), value_fn!(f64), value_fn!(f64), value_fn!(Color)],
),
)],
vec![(
NodeIdentifier::new("graphene_std::brush::ReduceNode<_, _>"),
|args| {
let acc: DowncastBothNode<(), ImageFrame> = DowncastBothNode::new(args[0]);
let image = acc.eval(());
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(BlendMode::Normal), ClonedNode::new(1.0));
let _ = &blend_node as &dyn for<'i> Node<'i, (Color, Color), Output = Color>;
let node = ReduceNode::new(ClonedNode::new(image), ValueNode::new(BlendImageTupleNode::new(ValueNode::new(blend_node))));
//let _ = &node as &dyn for<'i> Node<'i, core::slice::Iter<ImageFrame>, Output = ImageFrame>;
let any: DynAnyNode<Box<dyn Iterator<Item = ImageFrame> + Sync + Send>, _, _> = graphene_std::any::DynAnyNode::new(ValueNode::new(node));
Box::pin(any)
},
NodeIOTypes::new(concrete!(Box<dyn Iterator<Item = &ImageFrame> + Sync + Send>), concrete!(ImageFrame), vec![value_fn!(ImageFrame)]),
)],
// Filters
raster_node!(graphene_core::raster::LuminanceNode<_>, params: [LuminanceCalculation]),
raster_node!(graphene_core::raster::LevelsNode<_, _, _, _, _>, params: [f64, f64, f64, f64, f64]),
vec![(
NodeIdentifier::new("graphene_core::raster::BlendNode<_, _, _, _>"),
|args| {
use graphene_core::Node;
let image: DowncastBothNode<(), ImageFrame> = DowncastBothNode::new(args[0]);
let blend_mode: DowncastBothNode<(), BlendMode> = DowncastBothNode::new(args[1]);
let opacity: DowncastBothNode<(), f64> = DowncastBothNode::new(args[2]);
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(blend_mode.eval(())), ClonedNode::new(opacity.eval(())));
let node = graphene_std::raster::BlendImageNode::new(image, ValueNode::new(blend_node));
let _ = &node as &dyn for<'i> Node<'i, ImageFrame, Output = ImageFrame>;
let any: DynAnyNode<ImageFrame, _, _> = graphene_std::any::DynAnyNode::new(graphene_core::value::ValueNode::new(node));
any.into_type_erased()
},
NodeIOTypes::new(
concrete!(ImageFrame),
concrete!(ImageFrame),
vec![(concrete!(()), concrete!(ImageFrame)), (concrete!(()), concrete!(BlendMode)), (concrete!(()), concrete!(f64))],
vec![
(
NodeIdentifier::new("graphene_core::raster::BlendNode<_, _, _, _>"),
|args| {
let image: DowncastBothNode<(), ImageFrame> = DowncastBothNode::new(args[0]);
let blend_mode: DowncastBothNode<(), BlendMode> = DowncastBothNode::new(args[1]);
let opacity: DowncastBothNode<(), f64> = DowncastBothNode::new(args[2]);
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(blend_mode.eval(())), ClonedNode::new(opacity.eval(())));
let node = graphene_std::raster::BlendImageNode::new(image, ValueNode::new(blend_node));
let _ = &node as &dyn for<'i> Node<'i, ImageFrame, Output = ImageFrame>;
let any: DynAnyNode<ImageFrame, _, _> = graphene_std::any::DynAnyNode::new(graphene_core::value::ValueNode::new(node));
any.into_type_erased()
},
NodeIOTypes::new(concrete!(ImageFrame), concrete!(ImageFrame), vec![value_fn!(ImageFrame), value_fn!(BlendMode), value_fn!(f64)]),
),
)],
(
NodeIdentifier::new("graphene_core::raster::EraseNode<_, _>"),
|args| {
let image: DowncastBothNode<(), ImageFrame> = DowncastBothNode::new(args[0]);
let opacity: DowncastBothNode<(), f64> = DowncastBothNode::new(args[1]);
let blend_node = graphene_std::brush::EraseNode::new(ClonedNode::new(opacity.eval(())));
let node = graphene_std::raster::BlendImageNode::new(image, ValueNode::new(blend_node));
let _ = &node as &dyn for<'i> Node<'i, ImageFrame, Output = ImageFrame>;
let any: DynAnyNode<ImageFrame, _, _> = graphene_std::any::DynAnyNode::new(graphene_core::value::ValueNode::new(node));
any.into_type_erased()
},
NodeIOTypes::new(concrete!(ImageFrame), concrete!(ImageFrame), vec![value_fn!(ImageFrame), value_fn!(f64)]),
),
],
raster_node!(graphene_core::raster::GrayscaleNode<_, _, _, _, _, _, _>, params: [Color, f64, f64, f64, f64, f64, f64]),
raster_node!(graphene_core::raster::HueSaturationNode<_, _, _>, params: [f64, f64, f64]),
raster_node!(graphene_core::raster::InvertRGBNode, params: []),
@ -196,7 +270,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let any: DynAnyInRefNode<ImageFrame, _, _> = graphene_std::any::DynAnyInRefNode::new(node);
any.into_type_erased()
},
NodeIOTypes::new(generic!(T), concrete!(ImageFrame), vec![(concrete!(()), concrete!(ImageFrame))]),
NodeIOTypes::new(generic!(T), concrete!(ImageFrame), vec![value_fn!(ImageFrame)]),
),
(
NodeIdentifier::new("graphene_std::memo::EndLetNode<_>"),
@ -206,7 +280,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let any: DynAnyInRefNode<ImageFrame, _, _> = graphene_std::any::DynAnyInRefNode::new(node);
any.into_type_erased()
},
NodeIOTypes::new(generic!(T), concrete!(ImageFrame), vec![(concrete!(()), concrete!(VectorData))]),
NodeIOTypes::new(generic!(T), concrete!(ImageFrame), vec![value_fn!(VectorData)]),
),
(
NodeIdentifier::new("graphene_std::memo::RefNode<_, _>"),
@ -240,24 +314,24 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
concrete!(ImageFrame),
concrete!(ImageFrame),
vec![
(concrete!(()), concrete!(f64)),
(concrete!(()), concrete!(Option<DVec2>)),
(concrete!(()), concrete!(f64)),
(concrete!(()), concrete!(ImaginateSamplingMethod)),
(concrete!(()), concrete!(f64)),
(concrete!(()), concrete!(String)),
(concrete!(()), concrete!(String)),
(concrete!(()), concrete!(bool)),
(concrete!(()), concrete!(f64)),
(concrete!(()), concrete!(Option<Vec<u64>>)),
(concrete!(()), concrete!(bool)),
(concrete!(()), concrete!(f64)),
(concrete!(()), concrete!(ImaginateMaskStartingFill)),
(concrete!(()), concrete!(bool)),
(concrete!(()), concrete!(bool)),
(concrete!(()), concrete!(Option<std::sync::Arc<Image>>)),
(concrete!(()), concrete!(f64)),
(concrete!(()), concrete!(ImaginateStatus)),
value_fn!(f64),
value_fn!(Option<DVec2>),
value_fn!(f64),
value_fn!(ImaginateSamplingMethod),
value_fn!(f64),
value_fn!(String),
value_fn!(String),
value_fn!(bool),
value_fn!(f64),
value_fn!(Option<Vec<u64>>),
value_fn!(bool),
value_fn!(f64),
value_fn!(ImaginateMaskStartingFill),
value_fn!(bool),
value_fn!(bool),
value_fn!(Option<std::sync::Arc<Image>>),
value_fn!(f64),
value_fn!(ImaginateStatus),
],
),
),
@ -296,7 +370,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let node: DynAnyNode<&Image, _, _> = DynAnyNode::new(ValueNode::new(new_image));
node.into_type_erased()
},
NodeIOTypes::new(concrete!(Image), concrete!(Image), vec![(concrete!(()), concrete!(u32)), (concrete!(()), concrete!(f64))]),
NodeIOTypes::new(concrete!(Image), concrete!(Image), vec![value_fn!(u32), value_fn!(f64)]),
),
//register_node!(graphene_std::memo::CacheNode<_>, input: Image, params: []),
(
@ -307,7 +381,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let any = DynAnyRefNode::new(node);
any.into_type_erased()
},
NodeIOTypes::new(concrete!(()), concrete!(&Image), vec![(concrete!(()), concrete!(Image))]),
NodeIOTypes::new(concrete!(()), concrete!(&Image), vec![value_fn!(Image)]),
),
(
NodeIdentifier::new("graphene_std::memo::CacheNode"),
@ -317,7 +391,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let any = DynAnyRefNode::new(node);
any.into_type_erased()
},
NodeIOTypes::new(concrete!(()), concrete!(&ImageFrame), vec![(concrete!(()), concrete!(ImageFrame))]),
NodeIOTypes::new(concrete!(()), concrete!(&ImageFrame), vec![value_fn!(ImageFrame)]),
),
(
NodeIdentifier::new("graphene_std::memo::CacheNode"),
@ -327,7 +401,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let any = DynAnyRefNode::new(node);
any.into_type_erased()
},
NodeIOTypes::new(concrete!(ImageFrame), concrete!(&ImageFrame), vec![(concrete!(ImageFrame), concrete!(ImageFrame))]),
NodeIOTypes::new(concrete!(ImageFrame), concrete!(&ImageFrame), vec![fn_type!(ImageFrame, ImageFrame)]),
),
(
NodeIdentifier::new("graphene_std::memo::CacheNode"),
@ -337,7 +411,17 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
let any = DynAnyRefNode::new(node);
any.into_type_erased()
},
NodeIOTypes::new(concrete!(()), concrete!(&QuantizationChannels), vec![(concrete!(()), concrete!(QuantizationChannels))]),
NodeIOTypes::new(concrete!(()), concrete!(&QuantizationChannels), vec![value_fn!(QuantizationChannels)]),
),
(
NodeIdentifier::new("graphene_std::memo::CacheNode"),
|args| {
let input: DowncastBothNode<(), Vec<DVec2>> = DowncastBothNode::new(args[0]);
let node: CacheNode<Vec<DVec2>, _> = graphene_std::memo::CacheNode::new(input);
let any = DynAnyRefNode::new(node);
any.into_type_erased()
},
NodeIOTypes::new(concrete!(()), concrete!(&Vec<DVec2>), vec![value_fn!(Vec<DVec2>)]),
),
],
register_node!(graphene_core::structural::ConsNode<_, _>, input: Image, params: [&str]),
@ -357,6 +441,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
input: Vec<graphene_core::vector::bezier_rs::Subpath<graphene_core::uuid::ManipulatorGroupId>>,
params: [Vec<graphene_core::uuid::ManipulatorGroupId>]
),
register_node!(graphene_std::brush::VectorPointsNode, input: VectorData, params: []),
];
let mut map: HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstructor>> = HashMap::new();
for (id, c, types) in node_types.into_iter().flatten() {

View file

@ -2,8 +2,8 @@ use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{format_ident, ToTokens};
use syn::{
parse_macro_input, punctuated::Punctuated, token::Comma, FnArg, GenericParam, Ident, ItemFn, Lifetime, Pat, PatIdent, PathArguments, PredicateType, ReturnType, Token, TraitBound, Type, TypeParam,
TypeParamBound, WhereClause, WherePredicate,
parse_macro_input, punctuated::Punctuated, token::Comma, FnArg, GenericParam, Ident, ItemFn, Lifetime, Pat, PatIdent, PatType, PathArguments, PredicateType, ReturnType, Token, TraitBound, Type,
TypeParam, TypeParamBound, WhereClause, WherePredicate,
};
#[proc_macro_attribute]
@ -46,6 +46,20 @@ pub fn node_fn(attr: TokenStream, item: TokenStream) -> TokenStream {
panic!("Expected ident as primary input.");
};
let primary_input_ty = &primary_input.ty;
let aux_type_generics = type_generics
.iter()
.filter(|gen| {
if let GenericParam::Type(ty) = gen {
!function.sig.inputs.iter().take(1).any(|param_ty| match param_ty {
FnArg::Typed(pat_ty) => pat_ty.ty.to_token_stream().to_string() == ty.ident.to_string(),
_ => false,
})
} else {
false
}
})
.cloned()
.collect::<Vec<_>>();
let body = function.block;
@ -101,6 +115,7 @@ pub fn node_fn(attr: TokenStream, item: TokenStream) -> TokenStream {
}
});
let generics = type_generics.into_iter().chain(node_generics.iter().cloned()).collect::<Punctuated<_, Comma>>();
let new_fn_generics = aux_type_generics.into_iter().chain(node_generics.iter().cloned()).collect::<Punctuated<_, Comma>>();
// Bindings for all of the above generics to a node with an input of `()` and an output of the type in the function
let extra_where_clause = parameter_inputs
.iter()
@ -122,7 +137,7 @@ pub fn node_fn(attr: TokenStream, item: TokenStream) -> TokenStream {
})
})
.collect::<Vec<_>>();
where_clause.predicates.extend(extra_where_clause);
where_clause.predicates.extend(extra_where_clause.clone());
quote::quote! {
@ -140,7 +155,8 @@ pub fn node_fn(attr: TokenStream, item: TokenStream) -> TokenStream {
}
}
impl <#(#args),*> #node_name<#(#args),*>
impl <'input, #new_fn_generics> #node_name<#(#args),*>
where #(#extra_where_clause),*
{
pub const fn new(#(#parameter_idents: #struct_generics_iter),*) -> Self{
Self{