Browse Source

Some caching stuff for nodes

flabbet 7 months ago
parent
commit
5b9b9fee1c

+ 52 - 12
src/PixiEditor.ChangeableDocument/Changeables/Graph/FuncInputProperty.cs

@@ -11,13 +11,15 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph;
 public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncInputProperty
 public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncInputProperty
 {
 {
     private T? constantNonOverrideValue;
     private T? constantNonOverrideValue;
-    
-    internal FuncInputProperty(Node node, string internalName, string displayName, T defaultValue) : base(node, internalName, displayName, null)
+    private int lastConstantHashCode;
+
+    internal FuncInputProperty(Node node, string internalName, string displayName, T defaultValue) : base(node,
+        internalName, displayName, null)
     {
     {
         constantNonOverrideValue = defaultValue;
         constantNonOverrideValue = defaultValue;
         NonOverridenValue = _ => constantNonOverrideValue;
         NonOverridenValue = _ => constantNonOverrideValue;
     }
     }
-    
+
     protected internal override object FuncFactory(object toReturn)
     protected internal override object FuncFactory(object toReturn)
     {
     {
         Func<FuncContext, T> func = _ =>
         Func<FuncContext, T> func = _ =>
@@ -28,7 +30,7 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
                 shaderExpressionVariable.SetConstantValue(toReturn, ConversionTable.Convert);
                 shaderExpressionVariable.SetConstantValue(toReturn, ConversionTable.Convert);
                 return (T)(object)shaderExpressionVariable;
                 return (T)(object)shaderExpressionVariable;
             }
             }
-            
+
             return (T)toReturn;
             return (T)toReturn;
         };
         };
         return func;
         return func;
@@ -40,12 +42,12 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
         {
         {
             Type targetType = typeof(T);
             Type targetType = typeof(T);
             bool isShaderExpression = false;
             bool isShaderExpression = false;
-            if(typeof(T).IsAssignableTo(typeof(ShaderExpressionVariable)))
+            if (typeof(T).IsAssignableTo(typeof(ShaderExpressionVariable)))
             {
             {
                 targetType = targetType.BaseType.GenericTypeArguments[0];
                 targetType = targetType.BaseType.GenericTypeArguments[0];
                 isShaderExpression = true;
                 isShaderExpression = true;
             }
             }
-            
+
             var sourceObj = delegateToCast.DynamicInvoke(f);
             var sourceObj = delegateToCast.DynamicInvoke(f);
             ConversionTable.TryConvert(sourceObj, targetType, out var result);
             ConversionTable.TryConvert(sourceObj, targetType, out var result);
             if (isShaderExpression)
             if (isShaderExpression)
@@ -67,12 +69,12 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
 
 
                 return (T)toReturn;
                 return (T)toReturn;
             }
             }
-            
-            return result == null ? default : (T)result; 
+
+            return result == null ? default : (T)result;
         };
         };
         return func;
         return func;
     }
     }
-    
+
     private Expression Adjust(Expression expression, object toReturn, out bool adjustNestedVariables)
     private Expression Adjust(Expression expression, object toReturn, out bool adjustNestedVariables)
     {
     {
         adjustNestedVariables = false;
         adjustNestedVariables = false;
@@ -89,7 +91,7 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
 
 
         return expression;
         return expression;
     }
     }
-    
+
     private void AdjustNested(IMultiValueVariable toReturn, Expression expression)
     private void AdjustNested(IMultiValueVariable toReturn, Expression expression)
     {
     {
         if (toReturn is not ShaderExpressionVariable shaderExpressionVariable)
         if (toReturn is not ShaderExpressionVariable shaderExpressionVariable)
@@ -133,8 +135,8 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
             shaderExpressionVariable.SetConstantValue(value, ConversionTable.Convert);
             shaderExpressionVariable.SetConstantValue(value, ConversionTable.Convert);
             return;
             return;
         }
         }
-        
-        if(ConversionTable.TryConvert(value, typeof(T), out var result))
+
+        if (ConversionTable.TryConvert(value, typeof(T), out var result))
         {
         {
             constantNonOverrideValue = (T)result;
             constantNonOverrideValue = (T)result;
             return;
             return;
@@ -142,4 +144,42 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
 
 
         constantNonOverrideValue = default;
         constantNonOverrideValue = default;
     }
     }
+
+    internal override bool CacheChanged
+    {
+        get
+        {
+            if (constantNonOverrideValue == null)
+            {
+                return base.CacheChanged;
+            }
+
+            if (Connection == null && lastConnectionHash != -1)
+            {
+                return true;
+            }
+
+            if (Connection != null && lastConnectionHash != Connection.GetHashCode())
+            {
+                lastConnectionHash = Connection.GetHashCode();
+                return true;
+            }
+
+            if (constantNonOverrideValue is ShaderExpressionVariable expressionVariable)
+            {
+                return expressionVariable.ConstantValueString.GetHashCode() != lastConstantHashCode;
+            }
+
+            return base.CacheChanged;
+        }
+    }
+
+    internal override void UpdateCache()
+    {
+        base.UpdateCache();
+        if (constantNonOverrideValue is ShaderExpressionVariable expressionVariable)
+        {
+            lastConstantHashCode = expressionVariable.ConstantValueString.GetHashCode();
+        }
+    }
 }
 }

+ 16 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/InputProperty.cs

@@ -11,6 +11,7 @@ public class InputProperty : IInputProperty
 {
 {
     private object _internalValue;
     private object _internalValue;
     private int _lastExecuteHash = -1;
     private int _lastExecuteHash = -1;
+    protected int lastConnectionHash = -1;
     private PropertyValidator? validator;
     private PropertyValidator? validator;
     private IOutputProperty? connection;
     private IOutputProperty? connection;
 
 
@@ -86,10 +87,21 @@ public class InputProperty : IInputProperty
     public Node Node { get; }
     public Node Node { get; }
     public Type ValueType { get; }
     public Type ValueType { get; }
 
 
-    internal bool CacheChanged
+    internal virtual bool CacheChanged
     {
     {
         get
         get
         {
         {
+            if(Connection == null && lastConnectionHash != -1)
+            {
+                return true;
+            }
+            
+            if(Connection != null && lastConnectionHash != Connection.GetHashCode())
+            {
+                lastConnectionHash = Connection.GetHashCode();
+                return true;
+            }
+            
             if (Value is ICacheable cacheable)
             if (Value is ICacheable cacheable)
             {
             {
                 return cacheable.GetCacheHash() != _lastExecuteHash;
                 return cacheable.GetCacheHash() != _lastExecuteHash;
@@ -109,7 +121,7 @@ public class InputProperty : IInputProperty
         }
         }
     }
     }
 
 
-    internal void UpdateCache()
+    internal virtual void UpdateCache()
     {
     {
         if (Value is null)
         if (Value is null)
         {
         {
@@ -123,6 +135,8 @@ public class InputProperty : IInputProperty
         {
         {
             _lastExecuteHash = Value.GetHashCode();
             _lastExecuteHash = Value.GetHashCode();
         }
         }
+        
+        lastConnectionHash = Connection?.GetHashCode() ?? -1;
     }
     }
 
 
     IReadOnlyNode INodeProperty.Node => Node;
     IReadOnlyNode INodeProperty.Node => Node;

+ 6 - 14
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ModifyImageRightNode.cs

@@ -25,7 +25,9 @@ public class ModifyImageRightNode : RenderNode, IPairNode, ICustomShaderNode
     private string _lastSksl;
     private string _lastSksl;
     private VecI? size;
     private VecI? size;
 
 
-    protected override bool ExecuteOnlyOnCacheChange => true;
+    // TODO: Add caching
+    // Caching requires a way to check if any connected node changed, checking inputs for this node works
+    // Also gather uniforms without doing full string builder generation of the shader
 
 
     public ModifyImageRightNode()
     public ModifyImageRightNode()
     {
     {
@@ -33,9 +35,8 @@ public class ModifyImageRightNode : RenderNode, IPairNode, ICustomShaderNode
         Color = CreateFuncInput(nameof(Color), "COLOR", new Half4(""));
         Color = CreateFuncInput(nameof(Color), "COLOR", new Half4(""));
     }
     }
 
 
-    protected override void OnExecute(RenderContext renderContext)
+    protected override void OnPaint(RenderContext renderContext, DrawingSurface targetSurface)
     {
     {
-        base.OnExecute(renderContext);
         if (OtherNode == null || OtherNode == default)
         if (OtherNode == null || OtherNode == default)
         {
         {
             OtherNode = FindStartNode()?.Id ?? default;
             OtherNode = FindStartNode()?.Id ?? default;
@@ -57,7 +58,7 @@ public class ModifyImageRightNode : RenderNode, IPairNode, ICustomShaderNode
         {
         {
             return;
             return;
         }
         }
-        
+
         size = imgSize;
         size = imgSize;
 
 
         ShaderBuilder builder = new(size.Value);
         ShaderBuilder builder = new(size.Value);
@@ -107,17 +108,8 @@ public class ModifyImageRightNode : RenderNode, IPairNode, ICustomShaderNode
             drawingPaint.Shader = drawingPaint.Shader.WithUpdatedUniforms(builder.Uniforms);
             drawingPaint.Shader = drawingPaint.Shader.WithUpdatedUniforms(builder.Uniforms);
         }
         }
 
 
-        builder.Dispose();
-    }
-
-    protected override void OnPaint(RenderContext renderContext, DrawingSurface targetSurface)
-    {
-        if (size == null)
-        {
-            return;
-        }
-        
         targetSurface.Canvas.DrawRect(0, 0, size.Value.X, size.Value.Y, drawingPaint);
         targetSurface.Canvas.DrawRect(0, 0, size.Value.X, size.Value.Y, drawingPaint);
+        builder.Dispose();
     }
     }
 
 
     public override RectD? GetPreviewBounds(int frame, string elementToRenderName = "")
     public override RectD? GetPreviewBounds(int frame, string elementToRenderName = "")