Skip to content

Commit e4d2805

Browse files
TrueDoctorKeavon
andauthored
Add workaround for cache context nullification breaking nested loop indexes (#3178)
* Disable caching for the instance repeat etc. nodes * Add usize implementation for caching and monitor nodes * Change return type of instance index node back to f64 * Cleanup while loop * Remove stray usize changes --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
1 parent 390ce02 commit e4d2805

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

node-graph/gcore/src/context.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub trait ExtractAnimationTime {
2727
}
2828

2929
pub trait ExtractIndex {
30-
fn try_index(&self) -> Option<Vec<usize>>;
30+
fn try_index(&self) -> Option<impl Iterator<Item = usize>>;
3131
}
3232

3333
// Consider returning a slice or something like that
@@ -175,7 +175,7 @@ impl<T: ExtractAnimationTime + Sync> ExtractAnimationTime for Option<T> {
175175
}
176176
}
177177
impl<T: ExtractIndex> ExtractIndex for Option<T> {
178-
fn try_index(&self) -> Option<Vec<usize>> {
178+
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
179179
self.as_ref().and_then(|x| x.try_index())
180180
}
181181
}
@@ -212,7 +212,7 @@ impl<T: ExtractAnimationTime + Sync> ExtractAnimationTime for Arc<T> {
212212
}
213213
}
214214
impl<T: ExtractIndex> ExtractIndex for Arc<T> {
215-
fn try_index(&self) -> Option<Vec<usize>> {
215+
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
216216
(**self).try_index()
217217
}
218218
}
@@ -268,8 +268,8 @@ impl ExtractRealTime for ContextImpl<'_> {
268268
}
269269
}
270270
impl ExtractIndex for ContextImpl<'_> {
271-
fn try_index(&self) -> Option<Vec<usize>> {
272-
self.index.clone()
271+
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
272+
self.index.clone().map(|x| x.into_iter())
273273
}
274274
}
275275
impl ExtractVarArgs for ContextImpl<'_> {
@@ -304,8 +304,8 @@ impl ExtractAnimationTime for OwnedContextImpl {
304304
}
305305
}
306306
impl ExtractIndex for OwnedContextImpl {
307-
fn try_index(&self) -> Option<Vec<usize>> {
308-
self.index.clone()
307+
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
308+
self.index.clone().map(|x| x.into_iter())
309309
}
310310
}
311311
impl ExtractVarArgs for OwnedContextImpl {
@@ -418,7 +418,7 @@ impl OwnedContextImpl {
418418
footprint,
419419
varargs: None,
420420
parent,
421-
index,
421+
index: index.map(|x| x.collect()),
422422
real_time,
423423
animation_time,
424424
}

node-graph/gcore/src/vector/algorithms/instance.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::gradient::GradientStops;
22
use crate::raster_types::{CPU, Raster};
33
use crate::table::{Table, TableRowRef};
44
use crate::vector::Vector;
5-
use crate::{CloneVarArgs, Context, Ctx, ExtractAll, ExtractIndex, ExtractVarArgs, Graphic, InjectIndex, InjectVarArgs, OwnedContextImpl};
5+
use crate::{CloneVarArgs, Context, Ctx, ExtractAll, ExtractIndex, ExtractVarArgs, Graphic, InjectVarArgs, OwnedContextImpl};
66
use glam::DVec2;
77
use graphene_core_shaders::color::Color;
88

@@ -19,7 +19,7 @@ impl std::hash::Hash for HashableDVec2 {
1919

2020
#[node_macro::node(name("Instance on Points"), category("Instancing"), path(graphene_core::vector))]
2121
async fn instance_on_points<T: Into<Graphic> + Default + Send + Clone + 'static>(
22-
ctx: impl ExtractAll + CloneVarArgs + Sync + Ctx + InjectIndex + InjectVarArgs,
22+
ctx: impl ExtractAll + CloneVarArgs + Sync + Ctx + InjectVarArgs,
2323
points: Table<Vector>,
2424
#[implementations(
2525
Context -> Table<Graphic>,
@@ -63,7 +63,7 @@ async fn instance_on_points<T: Into<Graphic> + Default + Send + Clone + 'static>
6363

6464
#[node_macro::node(category("Instancing"), path(graphene_core::vector))]
6565
async fn instance_repeat<T: Into<Graphic> + Default + Send + Clone + 'static>(
66-
ctx: impl ExtractAll + CloneVarArgs + Ctx + InjectIndex,
66+
ctx: impl ExtractAll + CloneVarArgs + Ctx,
6767
#[implementations(
6868
Context -> Table<Graphic>,
6969
Context -> Table<Vector>,
@@ -106,9 +106,15 @@ async fn instance_position(ctx: impl Ctx + ExtractVarArgs) -> DVec2 {
106106
// TODO: Make this return a u32 instead of an f64, but we ned to improve math-related compatibility with integer types first.
107107
#[node_macro::node(category("Instancing"), path(graphene_core::vector))]
108108
async fn instance_index(ctx: impl Ctx + ExtractIndex, _primary: (), loop_level: u32) -> f64 {
109-
ctx.try_index()
110-
.and_then(|indexes| indexes.get(indexes.len().wrapping_sub(1).wrapping_sub(loop_level as usize)).copied())
111-
.unwrap_or_default() as f64
109+
let Some(index_iter) = ctx.try_index() else { return 0. };
110+
let mut last = 0;
111+
for (i, index) in index_iter.enumerate() {
112+
if i == loop_level as usize {
113+
return index as f64;
114+
}
115+
last = index;
116+
}
117+
last as f64
112118
}
113119

114120
#[cfg(test)]

0 commit comments

Comments
 (0)