From bc31fd83aea28f00c68f5d9726f3a61c81a21eaa Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Wed, 2 Apr 2025 08:24:41 +0200 Subject: [PATCH] WIP debugging --- node-graph/gcore/src/context.rs | 43 ++++++++++++++----- node-graph/gcore/src/memo.rs | 2 + node-graph/gcore/src/transform.rs | 49 ++++++++++++++++++---- node-graph/gstd/src/wasm_application_io.rs | 2 +- 4 files changed, 75 insertions(+), 21 deletions(-) diff --git a/node-graph/gcore/src/context.rs b/node-graph/gcore/src/context.rs index ae9fc66049..ef92b4d2bc 100644 --- a/node-graph/gcore/src/context.rs +++ b/node-graph/gcore/src/context.rs @@ -13,7 +13,7 @@ pub trait ExtractFootprint { fn footprint(&self) -> &Footprint { self.try_footprint().unwrap_or_else(|| { log::error!("Context did not have a footprint, called from: {}", Location::caller()); - &const { Footprint::empty() } + &Footprint::DEFAULT }) } } @@ -76,7 +76,7 @@ impl ExtractFootprint for Option { fn footprint(&self) -> &Footprint { self.try_footprint().unwrap_or_else(|| { log::warn!("trying to extract footprint from context None {} ", Location::caller()); - &const { Footprint::empty() } + &Footprint::DEFAULT }) } } @@ -193,7 +193,7 @@ impl ExtractFootprint for OwnedContextImpl { } impl ExtractTime for OwnedContextImpl { fn try_time(&self) -> Option { - self.time + self.real_time } } impl ExtractAnimationTime for OwnedContextImpl { @@ -245,10 +245,24 @@ pub struct OwnedContextImpl { parent: Option>, // This could be converted into a single enum to save extra bytes index: Option, - time: Option, + real_time: Option, animation_time: Option, } +// TODO: Probably delete this before committing +impl core::fmt::Debug for OwnedContextImpl { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("OwnedContextImpl") + .field("footprint", &self.footprint) + .field("varargs", &self.varargs) + // .field("parent", &self.parent) + .field("index", &self.index) + .field("real_time", &self.real_time) + .field("animation_time", &self.animation_time) + .finish() + } +} + impl Default for OwnedContextImpl { #[track_caller] fn default() -> Self { @@ -258,11 +272,14 @@ impl Default for OwnedContextImpl { impl core::hash::Hash for OwnedContextImpl { fn hash(&self, state: &mut H) { + debug!("Calculating hash for {:#?}", self); self.footprint.hash(state); + // TODO: Reenable before committing + // self.varargs.as_ref().map(|x| Arc::as_ptr(x).addr()).hash(state); + // self.parent.as_ref().map(|x| Arc::as_ptr(x).addr()).hash(state); self.index.hash(state); - self.time.map(|x| x.to_bits()).hash(state); - self.parent.as_ref().map(|x| Arc::as_ptr(x).addr()).hash(state); - self.varargs.as_ref().map(|x| Arc::as_ptr(x).addr()).hash(state); + self.real_time.map(|x| x.to_bits()).hash(state); + self.animation_time.map(|x| x.to_bits()).hash(state); } } @@ -279,7 +296,7 @@ impl OwnedContextImpl { varargs: None, parent, index, - time, + real_time: time, animation_time: frame_time, } } @@ -289,7 +306,7 @@ impl OwnedContextImpl { varargs: None, parent: None, index: None, - time: None, + real_time: None, animation_time: None, } } @@ -303,8 +320,8 @@ impl OwnedContextImpl { self.footprint = Some(footprint); self } - pub fn with_time(mut self, time: f64) -> Self { - self.time = Some(time); + pub fn with_real_time(mut self, time: f64) -> Self { + self.real_time = Some(time); self } pub fn with_animation_time(mut self, animation_time: f64) -> Self { @@ -314,6 +331,10 @@ impl OwnedContextImpl { pub fn into_context(self) -> Option> { Some(Arc::new(self)) } + pub fn erase_parent(mut self) -> Self { + self.parent = None; + self + } } #[derive(Default, Clone, Copy, dyn_any::DynAny)] diff --git a/node-graph/gcore/src/memo.rs b/node-graph/gcore/src/memo.rs index cdea7b6341..0643fa68df 100644 --- a/node-graph/gcore/src/memo.rs +++ b/node-graph/gcore/src/memo.rs @@ -25,9 +25,11 @@ where let mut hasher = DefaultHasher::new(); input.hash(&mut hasher); let hash = hasher.finish(); + if let Some(data) = self.cache.lock().as_ref().unwrap().as_ref().and_then(|data| (data.0 == hash).then_some(data.1.clone())) { Box::pin(async move { data }) } else { + debug!("Cache miss for {hash}"); let fut = self.node.eval(input); let cache = self.cache.clone(); Box::pin(async move { diff --git a/node-graph/gcore/src/transform.rs b/node-graph/gcore/src/transform.rs index 35ceba75ac..27f2952896 100644 --- a/node-graph/gcore/src/transform.rs +++ b/node-graph/gcore/src/transform.rs @@ -4,7 +4,9 @@ use crate::raster::bbox::AxisAlignedBbox; use crate::raster::image::ImageFrameTable; use crate::vector::VectorDataTable; use crate::{Artboard, ArtboardGroupTable, CloneVarArgs, Color, Context, Ctx, ExtractAll, GraphicGroupTable, OwnedContextImpl}; -use glam::{DAffine2, DVec2}; +use core::f64; +use core::hash::{Hash, Hasher}; +use glam::{DAffine2, DMat2, DVec2}; pub trait Transform { fn transform(&self) -> DAffine2; @@ -94,18 +96,26 @@ pub struct Footprint { impl Default for Footprint { fn default() -> Self { - Self::empty() + Self::DEFAULT } } impl Footprint { - pub const fn empty() -> Self { - Self { - transform: DAffine2::IDENTITY, - resolution: glam::UVec2::new(1920, 1080), - quality: RenderQuality::Full, - } - } + pub const DEFAULT: Self = Self { + transform: DAffine2::IDENTITY, + resolution: glam::UVec2::new(1920, 1080), + quality: RenderQuality::Full, + }; + + pub const BOUNDLESS: Self = Self { + transform: DAffine2 { + matrix2: DMat2::from_diagonal(DVec2::splat(f64::INFINITY)), + translation: DVec2::ZERO, + }, + resolution: glam::UVec2::new(0, 0), + quality: RenderQuality::Full, + }; + pub fn viewport_bounds_in_local_space(&self) -> AxisAlignedBbox { let inverse = self.transform.inverse(); let start = inverse.transform_point2((0., 0.).into()); @@ -198,3 +208,24 @@ fn replace_transform( } data } + +#[node_macro::node(category("Debug"))] +async fn boundless_footprint( + ctx: impl Ctx + CloneVarArgs + ExtractAll, + #[implementations( + Context -> VectorDataTable, + Context -> GraphicGroupTable, + Context -> ImageFrameTable, + Context -> TextureFrameTable, + )] + transform_target: impl Node, Output = Instances>, +) -> Instances { + let ctx = OwnedContextImpl::from(ctx).with_footprint(Footprint::BOUNDLESS).with_real_time(0.).erase_parent(); + + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + ctx.hash(&mut hasher); + let hash = hasher.finish(); + debug!("The context with hash {hash} is now: {ctx:#?}"); + + transform_target.eval(ctx.into_context()).await +} diff --git a/node-graph/gstd/src/wasm_application_io.rs b/node-graph/gstd/src/wasm_application_io.rs index 964b5db4f4..987d367054 100644 --- a/node-graph/gstd/src/wasm_application_io.rs +++ b/node-graph/gstd/src/wasm_application_io.rs @@ -237,7 +237,7 @@ async fn render<'a: 'n, T: 'n + GraphicElementRendered + WasmNotSend>( let footprint = render_config.viewport; let ctx = OwnedContextImpl::default() .with_footprint(footprint) - .with_time(render_config.time.time) + .with_real_time(render_config.time.time) .with_animation_time(render_config.time.animation_time.as_secs_f64()) .into_context(); ctx.footprint();